Dictionaries in Python

Dictionaries in Python

ยท

1 min read

What are Dictionaries in Python?

Dictionaries are a collection of (key, value) pairs. Value can be accessed using the corresponding key.

In the Lists, we accessed the values based on their index position but In, dictionaries we use the key to access the value.

Example

prices={'apples'=20,'oranges'=30,'grapes'=50}
print['apples']       
# 20  

Values in dictionaries can be of any type like integers, float, lists, and other dictionaries as well.

Example

complex_dict2={'k4':[1,7,5,0],'k5':{'k2':2}}
print(complex_dict2['k4'][3])          
# 0

A new key-value can be added to an existing dictionary and we can also update the associated with the key. To access all the keys in a dictionary using the (keys()) method, to get all the values to use the (value()) method.

ย