Prahlad Inala
Prahlad Inala

Follow

Prahlad Inala

Follow
Dictionaries in Python

Dictionaries in Python

Prahlad Inala's photo
Prahlad Inala
ยทJul 7, 2021ยท

1 min read

Play this article

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.

ย 
Share this