Python Dictionaries


Dictionaries

The Python data type dictionary is an unordered set of keys and values. The values can be searched using the keys, which can be any immutable type; strings and numbers can always be keys.

Examples

Extract values from a dictionary

>>> my_dict = {'a1': 12, 'a2': 26, 'a3': 33}
>>> my_dict
{'a3': 33, 'a1': 12, 'a2': 26}
>>> my_dict['a2']
26
>>> 

Eliminate element from dictionary

>>> my_dict = {'a1': 12, 'a2': 26, 'a3': 33}
>>> my_dict
{'a3': 33, 'a1': 12, 'a2': 26}
>>> del my_dict['a2']
>>> my_dict
{'a3': 33, 'a1': 12}
>>> 

List and sort keys from a dictionary

>>> my_dict = {'a1': 24, 'a2': 35, 'a3': 78}
>>> my_dict
{'a3': 78, 'a1': 24, 'a2': 35}
>>> list(my_dict.keys())
['a3', 'a1', 'a2']
>>> sorted(my_dict.keys())
['a1', 'a2', 'a3']
>>>