A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values. A dictionary is like an address-book where you can find the address or contact details of a person by knowing only his/her name i.e. we associate keys (name) with values (details).
1. CREATE A DICTIONARY
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(thisdict)
OUTPUT:

2. ACCESSING ITEMS OF DICTIONARY IN PYTHON 3
2.1 ACCESSING ITEMS USING ITS KEY NAME
Referring to its key name, inside square brackets.
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } x = thisdict["model"] print(x)
OUTPUT:

2.2 ACCESSING ITEMS USING get() METHOD
This will give you the same result
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } x = thisdict.get("model") print(x)
OUTPUT:

3. CHANGE VALES OF DICTIONARY IN PYTHON
You can change the value of a specific item by referring to its key name:
Example: Change the “year” to 2018:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["year"] = 2018
4. ITERATE OVER A DICTIONARY IN PYTHON 3
You can Iterate over (looping) a dictionary by using for loop.
When looping through a dictionary, the return values are the keys of the dictionary, but there are methods to return the values as well.
4.1 PRINT ALL KEY NAMES IN THE DICTIONARY, one by one
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } for x in thisdict: print(x)
OUTPUT:

4.2 PRINT ALL VALUES IN THE DICTIONARY, one by one
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } for x in thisdict: print(thisdict[x])
OUTPUT:

4.3 USE FUNCTION “values()” TO RETURN VALUES OF A DICTIONARY
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } for x in thisdict.values(): print(x)
OUTPUT:

4.4 USE FUNCTION “items()” FOR BOTH KEYS AND VALUES IN DICTIONARY
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } for x, y in thisdict.items(): print(x, y)
OUTPUT:

5. CHECK IF KEY EXISTS
Check if “model” is present in the dictionary
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")
OUTPUT:

6. DICTIONARY LENGTH
To determine how many items (key-value pairs) a dictionary has, use the len() method.
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(len(thisdict))
OUTPUT:

7. ADDING ITEMS
Adding an item to the dictionary is done by using a new index key and assigning a value to it.
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict["color"] = "red" print(thisdict)
OUTPUT:

8. REMOVING ITEMS
There are several methods to remove items from a dictionary:
8.1 pop() –
The pop() method removes the item with the specified key name
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict.pop("model") print(thisdict)
OUTPUT:

8.2 popitem() –
The popitem() method removes the last inserted item.
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict.popitem() print(thisdict)
OUTPUT:

8.3 del –
The del
keyword removes the item with the specified key name:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
OUTPUT:

8.4 del –
The del
keyword can also delete the dictionary completely:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict
print(thisdict) #this will cause an error because "thisdict" no longer exists.
OUTPUT:

8.5 clear()
The clear()
method empties the dictionary
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict)
OUTPUT:

9. COPY a DICTIONARY
You cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only be a reference to dict1, and changes made in dict1 will automatically also be made in dict2.
9.1 BUILT-IN DICTIONARY METHOD copy()
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
print(mydict)
OUTPUT:

9.2 COPY DICTIONARY IN PYTHON USING dict() METHOD
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = dict(thisdict)
print(mydict)
OUTPUT:
