33 - Dictionaries
π§Ά Tags:: #Python_Basics
Up:: 34 - Dictionary Methods
Down:: 32 - Set Methods
π Resources:: Playlist
2023-12-31 - 00:02
Dictionaries are an ordered collection of data items. They store multiple items in a single variable. Dictionary items are key-value pairs that are separated by commas and enclosed within curly brackets {}.
Example:
info = {'name':'Karan', 'age':19, 'eligible':True}
print(info)
Output:
{'name': 'Karan', 'age': 19, 'eligible': True}
Accessing Dictionary items:
I. Accessing single values:
Values in a dictionary can be accessed using keys. We can access dictionary values by mentioning keys either in square brackets or by using get method.
Example:
info = {'name':'Karan', 'age':19, 'eligible':True}
print(info['name'])
print(info.get('eligible'))
Output:
Karan
True
II. Accessing multiple values:
We can print all the values in the dictionary using values() method.
Example:
info = {'name':'Karan', 'age':19, 'eligible':True}
print(info.values())
Output:
dict_values(['Karan', 19, True])
III. Accessing keys:
We can print all the keys in the dictionary using keys() method.
Example:
info = {'name':'Karan', 'age':19, 'eligible':True}
print(info.keys())
Output:
dict_keys(['name', 'age', 'eligible'])
IV. Accessing key-value pairs:
We can print all the key-value pairs in the dictionary using items() method.
Example:
info = {'name':'Karan', 'age':19, 'eligible':True}
print(info.items())
Output:
dict_items([('name', 'Karan'), ('age', 19), ('eligible', True)])
Main Example
info = {'name':'Karan', 'age':19, 'eligible':True}
# print(info)
# print(info.keys())
# print(info.values())
# for key in info.keys():
# print(f"The value corresponding to the key {key} is {info[key]}")
print(info.items())
for key, value in info.items():
print(f"The value corresponding to the key {key} is {value}")