Register Login

Python Dictionary

Updated Mar 21, 2022

Have your teacher ever called you by your unique roll number and not your name? Yes Python also provides the same concept where your values will be called by their unique keys. Python provides numerous data structures to store data. The dictionary is one of those data structures which are extremely useful to Python users.

What is a Python dictionary?

Python dictionaries store data as maps (mappings of keys to values). These dictionaries keep data values in key:value pairs. Keys in Python dictionaries do not support polymorphism. A Python dictionary enables reading, changing data, and updating data effortlessly.
Dictionaries are within curly braces. A comma (,) separates the key-value pairs in the dictionary, and a colon (:) splits every key from its value.

Example:

Below is a dictionary that contains the population data of the five largest cities in India.

Population = {'Mumbai': 12000000 , 'Delhi': 10000000 , 'Bangalore': 5000000 , 'Kolkata': 4000000 , 'Chennai': 4657500.}

Creating a Python dictionary

Creating a dictionary in Python is very effortless. One way is the common process that we use by putting the key and value pair within curly braces. The other way is when Python provides a built in function dict() to create a dictionary. Here is an example:

Code:

student_age = dict(John = 16, Kyle = 20, Tina = 18, Sushma = 19)
print(student_age)

Output:

 

How to insert elements in a Python dictionary?

Below is an example that shows how to add elements to a dictionary in Python:

Code:

student_age = dict(John = 16, Kyle = 20, Tina = 18, Sushma = 19)
print("Before adding an element to the dictionary")
print(student_age)
student_age['Sreejoni'] = 19
student_age['chinmui'] = 18
print("After adding elements to the dictionary")
print(student_age)

Output:

We can insert multiple items using the function dict.update()

Code:

student_age = dict(Ananda = 16, Joydeep = 20, Tina = 18, Neha = 19)
print("Before adding an element to the dictionary")
print(student_age)
student_age.update(Chinmui = 19, debojoti = 20, Sagar = 19, Vishal = 18)
print("After adding multiple elements to the dictionary")
print(student_age)

Output:

Check the existence of a key in a Python dictionary:

There are three methods to check the existence of a key in a particular Python dictionary:

With dictionary.keys()

This method returns all the keys available in the dictionary. It uses the if statement along with an ‘in’ operator. Below is an example:

Code:

def check(dictionary, key):
 if key in dictionary.keys():
    print("PRESENT", end = " ")
    print("The value =", dictionary[key])
 else:
    print("The key is not present")

dictionary = {'a': 500, 'b':600, 'c':700}
key = 'a'
check(dictionary, key)
key = 'x'
check(dictionary, key)

Output:

With If statement and in operator

This method operates with the if statement to inspect if the provided key exists in the dictionary.

Code:

def check(dictionary, key):
 if key in dictionary:
    print("PRESENT", end =" ")
    print("the value =", dictionary[key])
 else:
    print("the key is not present")

dictionary = {'a': 500, 'b':600, 'c':700}
key = 'b'
check(dictionary, key)
key = 'x'
check(dictionary, key)

Output:

With has_keys()

This method is not that conventional to use for regular programming or development purposes. It shows an error if you use an older version of Python:

Code:

dict = {'a': 100, 'b':200, 'c':300}
if dict.has_key('one'):
    print("Key exists")
else:
    print("Key does not exist")

Output:

How to change the elements in a Python dictionary

We can change or update elements to a dictionary using square brackets ([]) by accessing the key. Below is an example:

Code:

student_age = dict(John = 16, Kyle = 20, Meena = 18, Sushma = 19)
print("Before changing the elements of the dictionary")
print(student_age)
student_age['Ananda']=18
print("After changing the elements of the dictionary")
print(student_age)

Output:

And to change multiple items of a dictionary, we can use the in_built update() function:

Code:

student_age = dict(Ananda=16, Joydeep=20, Tina=18, Neha=19)
print("Before changing the elements of the dictionary")
print(student_age)
student_age.update(Ananda = 18, Joydeep = 21, Tina = 19) 
print("After changing the elements of the dictionary")
print(student_age)

Output:

Conclusion:

Python dictionaries store data values in key:value pairs. Dictionary is mostly used for creating the database software. Keys in Python dictionaries do not support polymorphism. A comma (,) separates the key-value pairs in the dictionary, and a colon (:) split every key from its value. But make sure, dictionary is slower than tuples and lists because it caters to two values to represent one element – which ultimately reduce down the efficiency of the program.


×