DICTIONARIES IN PYTHON


In this tutorial, we are going to learn about dictionaries in python and figure out the different constituents that are required to create a dictionary like key value pairs


What are Dictionaries in Python?

A dictionary is an unordered, mutable, and indexed collection. Dictionaries are created by using curly brackets in Python, and they consist of key value pairs. Let’s create a simple dictionary to understand more:

book= { "pages": "277", "name": "Gone Girl", "year": 2007 } 
print(book)

In the above example we have seen that we have created a dictionary known as book, which consists of three key-value pairs where pages, name and year are the keys and 277, Gone Girl and 2007 are their respective values. Output will be:

{'pages': '277', 'name': 'Gone Girl', 'year': 2007}

Accessing Elements inside a Dictionary

We can always access the elements inside the dictionary by using the ‘key’ name to access the ‘value’ of a dictionary.

book= { "pages": "277", "name": "Gone Girl", "year": 2007 } x = book['name'] print(x)

Changing Elements inside a Dictionary

You can change the values of a particular by referring the item to its key. For Example:

book=  { "pages": "277", "name": "Gone Girl", "year": 2007 } book['year'] = '2010' print(book)




Using Loop to print keys and values

You can print all the keys by using a simple for loop, for example:

book= { "pages": "277", "name": "Gone Girl", "year": 2007 } for n in book: print(n)

You can also print values separately which is not possible in using a regular for loop (which only prints keys), so in order to print values you can use the values() method at the end of the dictionary’s name, for example:

book= { "pages": "277", "name": "Gone Girl", "year": 2007 } for n in book.values(): print(n)

If you want to print both keys and values together as a key-value pair using a for loop, you can do that as well by using two variables n and m that will get stored in items() methods, for example:

book= { "pages": "277", "name": "Gone Girl", "year": 2007 } for n, m in book.items(): print(n,m)

Removing Items from a Dictionary

You can pop items in a dictionary as well by using the pop() function. For example if you want to pop out name of the book, then you can do that by naming ‘name’ inside the pop() parentheses.

book= { "pages": "277", "name": "Gone Girl", "year": 2007 } book.pop('name') print(book)

You can use the del function as well to delete an item inside a dictionary or the whole dictionary as well

book= { "pages": "277", 
"name": "Gone Girl", 
"year": 2007 } 
del book['pages'] 
print(book)

Output will be:

{'name': 'Gone Girl', 'year': 2007}

Similarly, you can delete the whole dictionary as well by using the del keyword:

book= { "pages": "277", 
"name": "Gone Girl", 
"year": 2007 } 
del book 
print(book)

Output will be:

Traceback (most recent call last):
  File "/tmp/sessions/11ab166edc475687/main.py", line 7, in 
    print(book)
NameError: name 'book' is not defined

For more references, click here