Dictionaries In Python
Python Dictionaries: Comprehensive Theory
Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered*, changeable, and does not allow duplicates.
1. Characteristics of Dictionaries
- Ordered: As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.
- Changeable: We can change, add, or remove items after the dictionary has been created.
- Duplicates Not Allowed: Dictionaries cannot have two items with the same key. If you try to add a duplicate key, the new value will overwrite the old one.
2. Creating a Dictionary
Dictionaries are written with curly brackets {}, and have keys and values.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
dict()
It is also possible to use the dict() constructor to make a dictionary.
thisdict = dict(name = "John", age = 36, country = "Norway")
3. Accessing Items
You can access the items of a dictionary by referring to its key name, inside square brackets [].
model = thisdict["model"]
get()
There is also a method called get() that will give you the same result, but it returns None instead of an error if the key doesn't exist.
model = thisdict.get("model")
4. Dictionary Methods for Values/Keys
keys(): Returns a list of all the keys in the dictionary.values(): Returns a list of all the values in the dictionary.items(): Returns a list of tuples containing each key-value pair.
x = thisdict.keys()
y = thisdict.values()
z = thisdict.items()
5. Modifying & Adding Items
Change Value
You can change the value of a specific item by referring to its key name:
thisdict["year"] = 2018
# OR use update()
thisdict.update({"year": 2020})
Adding Items
Adding an item to the dictionary is done by using a new index key and assigning a value to it:
thisdict["color"] = "red"
6. Removing Items
pop(key): Removes the item with the specified key name.popitem(): Removes the last inserted item.del: Thedelkeyword removes the item with the specified key name or deletes the dictionary entirely.clear(): Empties the dictionary.
7. Nested Dictionaries
A dictionary can contain dictionaries, this is called nested dictionaries.
myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
}
}
8. Dictionary Methods Table
| Method | Description |
| :--- | :--- |
| clear() | Removes all the elements from the dictionary |
| copy() | Returns a copy of the dictionary |
| fromkeys() | Returns a dictionary with the specified keys and value |
| get() | Returns the value of the specified key |
| items() | Returns a list containing a tuple for each key value pair |
| keys() | Returns a list containing the dictionary's keys |
| pop() | Removes the element with the specified key |
| popitem() | Removes the last inserted key-value pair |
| setdefault() | Returns the value of the specified key. If the key does not exist: insert the key, with the specified value |
| update() | Updates the dictionary with the specified key-value pairs |
| values() | Returns a list of all the values in the dictionary |