📓 Dictionaries¶
1. Definition and Initialization¶
In Python, a dictionary is an unordered collection of key-value pairs. Each key-value pair is separated by a colon ":" and the pairs are separated by commas. Dictionaries are written with curly brackets.
# dictionary of integer keys and string values
d1 = {1: 'apple', 2: 'banana', 3: 'cherry'}
print(d1) # prints: {1: 'apple', 2: 'banana', 3: 'cherry'}
# dictionary of string keys and integer values
d2 = {'one': 1, 'two': 2, 'three': 3}
print(d2) # prints: {'one': 1, 'two': 2, 'three': 3}
# mixed dictionary
d3 = {1: 'apple', 'two': 2, True: 3.14}
print(d3) # prints: {1: 'apple', 'two': 2, True: 3.14}
2. Accessing Dictionary Items¶
You can access dictionary items by referring to their key name.
d1 = {1: 'apple', 2: 'banana', 3: 'cherry'}
print(d1[1]) # prints: 'apple'
3. Modifying Dictionary Items¶
You can modify the value of a specific item by referring to its key name.
d1 = {1: 'apple', 2: 'banana', 3: 'cherry'}
d1[1] = 'avocado'
print(d1) # prints: {1: 'avocado', 2: 'banana', 3: 'cherry'}
4. Adding Items¶
Adding an item to the dictionary is done by using a new index key and assigning a value to it.
d1 = {1: 'apple', 2: 'banana', 3: 'cherry'}
d1[4] = 'dragonfruit'
print(d1) # prints: {1: 'apple', 2: 'banana', 3: 'cherry', 4: 'dragonfruit'}
5. Removing Items¶
There are several methods to remove items from a dictionary, such as del
keyword, pop()
, popitem()
, and clear()
.
d1 = {1: 'apple', 2: 'banana', 3: 'cherry', 4: 'dragonfruit'}
del d1[1]
print(d1) # prints: {2: 'banana', 3: 'cherry', 4: 'dragonfruit'}
d1.pop(2)
print(d1) # prints: {3: 'cherry', 4: 'dragonfruit'}
d1.popitem() # removes the last inserted item
print(d1) # prints: {3: 'cherry'}
d1.clear()
print(d1) # prints: {}
6. Dictionary Methods¶
Python provides several methods to manipulate dictionaries, like keys()
, values()
, items()
, get()
, update()
, and many more.
d1 = {1: 'apple', 2: 'banana', 3: 'cherry'}
print(d1.keys()) # prints: dict_keys([1, 2, 3])
print(d1.values()) # prints: dict_values(['apple', 'banana', 'cherry'])
print(d1.items()) # prints: dict_items([(1, 'apple'), (2, 'banana'), (3, 'cherry')])
# get() returns the value for the specified key if key is in dictionary.
print(d1.get(1)) # prints: 'apple'
print(d1.get(4)) # prints: None
# update() method updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs.
d1.update({4: 'dragonfruit'})
print(d1) # prints: {1: 'apple', 2: 'banana', 3: 'cherry', 4: 'dragonfruit'}
7. Check if Key Exists¶
To determine if a specified key is present in a dictionary, you can use the in
keyword.
d1 = {1: 'apple', 2: 'banana', 3: 'cherry'}
print(1 in d1) # prints: True
print(4 in d1) # prints: False
8. Length of a Dictionary¶
You can get the number of items (key-value pairs) in a dictionary with the len()
function.
d1 = {1: 'apple', 2: 'banana', 3: 'cherry'}
print(len(d1)) # prints: 3
9. Nested Dictionaries¶
A dictionary can contain dictionaries, this is called nested dictionaries.
nested_dict = {1: 'apple', 2: 'banana', 3: {'name': 'cherry', 'color': 'red'}}
print(nested_dict) # prints: {1: 'apple', 2: 'banana', 3: {'name': 'cherry', 'color': 'red'}}
10. Dictionary Comprehension¶
Dictionary comprehension is an elegant and concise way to create a new dictionary from an iterable in Python.
# create a dictionary with integer keys and their corresponding square values
squares = {x: x**2 for x in range(6)}
print(squares) # prints: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}