Dictionary

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:

Output - Create dictionary in python
Output – Create dictionary in python

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:

Output - Accessing Dictionary Item using Key
Output – Accessing Dictionary Item using Key

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:

Output - Accessing Dictionary Item using get() method
Output – Accessing Dictionary Item using get() method

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:

Output - Print all key names in the DICTIONARY in Python
Output – Print all key names in the DICTIONARY in Python

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:

 

Output - Print all Values in the DICTIONARY in Python
Output – Print all Values in the DICTIONARY in Python

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:

Output - USE FUNCTION “values()” TO RETURN VALUES OF A DICTIONARY
Output – USE FUNCTION “values()” TO RETURN VALUES OF A DICTIONARY

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:

OUTPUT - USE FUNCTION “items()” FOR BOTH KEYS AND VALUES IN DICTIONARY
OUTPUT – USE FUNCTION “items()” FOR BOTH KEYS AND VALUES IN DICTIONARY

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:

OUTPUT - CHECK IF KEY EXISTS IN DICTIONARY IN PYTHON
OUTPUT – CHECK IF KEY EXISTS IN DICTIONARY IN PYTHON

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:

OUTPUT - DICTIONARY LENGTH IN PYTHON
OUTPUT – DICTIONARY LENGTH IN PYTHON

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:

OUTPUT - ADDING ITEMS IN DICTIONARY IN PYTHON
OUTPUT – ADDING ITEMS IN DICTIONARY IN PYTHON

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:

OUTPUT - POP BY KEY IN DICTIONARY IN PYTHON
OUTPUT – POP BY KEY IN DICTIONARY IN PYTHON

8.2 popitem() – 

The popitem() method removes the last inserted item. 

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.popitem()
print(thisdict)

OUTPUT:

OUTPUT - POPITEM BY VALUE IN DICTIONARY IN PYTHON
OUTPUT – POPITEM BY VALUE IN DICTIONARY IN PYTHON

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:

OUTPUT - DELETE ITEM BY KEY IN DICTIONARY IN PYTHON
OUTPUT – DELETE ITEM BY KEY IN DICTIONARY IN PYTHON

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:

OUTPUT - DELETE DICTIONARY IN PYTHON
OUTPUT – DELETE DICTIONARY IN PYTHON

8.5 clear()

The clear() method empties the dictionary

thisdict =	{
  "brand""Ford",
  "model""Mustang",
  "year"1964
}
thisdict.clear()
print(thisdict)

OUTPUT:

OUTPUT - CLEAR DICTIONARY IN PYTHON
OUTPUT – CLEAR DICTIONARY IN PYTHON

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:

OUTPUT - CLEAR DICTIONARY IN PYTHON
OUTPUT – CLEAR DICTIONARY IN PYTHON

9.2 COPY DICTIONARY IN PYTHON USING dict() METHOD

thisdict =	{
  "brand""Ford",
  "model""Mustang",
  "year"1964
}
mydict = dict(thisdict)
print(mydict)

OUTPUT:

OUTPUT - COPY DICTIONARY IN PYTHON USING DICT
OUTPUT – COPY DICTIONARY IN PYTHON USING DICT

Leave a Reply

Your email address will not be published. Required fields are marked *