An associative array with mapped key: value pair
my_dict = ["Daniel": 13, "Jose": 25]
A dictionary can contain any inmmutable value as it's keys. So you can use tuples as long as they do not conatin mutalbe objects either directly or indirectly
Keys must be unique within one dictionary. You can also use comprehension to create dictionaries
You can use the list()
, built-in function to return a list of all the keys
defined in the dictionary in inserti order
tel = {'jack': 4098, 'guido': 4139, 'irv': 3000}
list(tel) # ['jack', 'guido', 'irv']
- To view a sorted list of all the keys use
sorted()
tel = {'jack': 4098, 'guido': 4139, 'irv': 3000}
sorted(tel) # ['guido', 'irv', 'jack']
You can change the value pair of a given key by referencing it
tel = {'jack': 4098, 'sape': 4139, 'guido': 4000}
tel["guido"] = 4127
print(tel) # {'jack': 4098, 'sape': 4139, 'guido': 4127}
You can delete a key and it's value with the del
keyword
tel = {'jack': 4098, 'sape': 4139}
del tel["sape"]
print(tel) # {'jack': 4098}
Normally you wouldn't be able to pass an iterable as the key of a dictionary, other than strings, since they keys have to be inmutable and list an other iterables tend to be mutable
You can use frozensets to pass a set, an iterable, as the key of a dictionary
key_1 = frozenset([1, 2])
key_2 = frozenset([3, 4])
my_dict = {key_1: 'good', key_2: 'bad'}
pairs = []
for key, value in my_dict.items():
for element in key:
pairs.append((element, value))
# [(1, 'good'), (2, 'good'), (3, 'bad'), (4, 'bad')]
You can use the dict()
built-in function to create dictionaries from key:
value pairs
tel = dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
print(tel) # {'sape': 4139, 'guido': 4127, 'jack': 4098}
Following the same process from the past recipe we can use the zip()
function to build the list of key: value
pairs from two
separete lists
names = ["Daniel", "Jose"]
ages = [23, 22]
my_dict = dict(zip(names, ages))
print(my_dict) # {'Daniel': 23, 'Jose': 22}
When looping through dictionaries, the key and value can be
retrieved at the same time using the items()
method.
knights = {'gallahad': 'the pure', 'robin': 'the brave'}
for k, v in knights.items():
print(k, v)
# gallahad the pure
# robin the brave
The default way of looping over a dictionary is doing it by it's keys
for key in my_dict:
print(key)
You can get a list of all the values in your dictionary with the values()
method
for value in my_dict.values():
print(value)