How to use dictionaries in Python

 
Video Tutorials
Artificial Intelligence Tutorial
Machine Learning Projects TutorialPython Video tutorial
Computer Graphics Video TutorialC++ Video Tutorial
Big Data Analytics Video TutorialSoft Computing Video Tutorial
Placement Video TutorialJava Video Tutorial

How to use dictionaries in Python

A Dictionaries in python is like a list, but more general. In a list, the index positions have to be integers; in a dictionary, the indices can be (almost) any type. That is index can be integer, string, character, or double type.

Video tutorial

Lists Vs. Dictionaries in Python

In lists, the values are stored at numeric indexes.

In Dictionaries, we can choose the indexes for the values. Indexes are

known as keys and elements as values in dictionaries

Lists are created either using list() function or pair of [ ] brackets.

Dictionaries are created using either dict() function or pair of { } with : for separating the key and value pair.

Example of how to create and use dictionaries in Python

#Empty Dictionary

d1 = {}
print (d1)
{}
#Empty Dictionary

d1 = dict()
print (d1)

Output

{}

Print entire Dictionary using print statement

The example shows how to display the entire dictionary using the print statement in python. First, we have created a dictionary with three elements and the print statement is used to print the elements of a dictionary. Next, the new element with key as mobile and the value as 9797979797 is added to the dictionary. The updated dictionary is printed using a print statement. Finally, the statement shows how to extract the value present at the key index. The value present at the key position name is Rahul.

d1 = dict()
d1 = {'fname':'Rahul', 'lname':'Dravid', 'dept':'CSE'}
print (d1)
d1['mobile'] = 9797979797
print (d1)

print (d1['fname'])

Output

{'fname': 'Rahul', 'lname': 'Dravid', 'dept': 'CSE'}
{'fname': 'Rahul', 'lname': 'Dravid', 'dept': 'CSE', 'mobile': 9797979797}
Rahul

The list can contain heterogeneous kinds of elements for keys and values. The example shows some elements have numeric keys some have strings as keys. Similarly, some values are numeric, and other is string type.

#Combination of elements Dictionary

d1 = {'fname':'Rahul', 1:'Dravid', 'dept':'CSE', 'mobile':9797979797}
print (d1)

Output

{'fname': 'Rahul', 1: 'Dravid', 'dept': 'CSE', 'mobile': 9797979797}

Length of dictionary

The length of the dictionary can be calculated using len() function. The following example shows how to use len() function.

#Print length of Dictionary

d1 = {'fname':'Rahul', 1:'Dravid', 'dept':'CSE', 'mobile':9797979797}
print ("Length of ", d1, "is", len(d1))

Output

Length of  {'fname': 'Rahul', 1: 'Dravid', 'dept': 'CSE', 'mobile': 9797979797} is 4

Extract Keys and Values from dictionary in python

Sometimes we want to extract the keys and values of the dictionary.

keys() function is used to extract the keys of a dictionary.

values function is used to extract the values of the dictionary.

Following example demonstrate the useage of keys() and vales() functions.

#Extract Keys

d1 = {'fname':'Rahul', 1:'Dravid', 'dept':'CSE', 'mobile':9797979797}
keys = list(d1.keys())
print (keys)
print (keys[1])

Output

['fname', 1, 'dept', 'mobile']
1
#Extract Values

d1 = {'fname':'Rahul', 1:'Dravid', 'dept':'CSE', 'mobile':9797979797}
values = list(d1.values())
print (values)

Output

['Rahul', 'Dravid', 'CSE', 9797979797]

in operator on dictionaries in python

in operator on dictionaries is used to find whether the given key is present in the dictionary or not. If the key is present, in operator return True otherwise False. The in operator works on keys but not on values. If one wants to know whether the value is present in the list using in operator, first we need to get the values of the dictionary into a list. Then use in operator.

# in operator (Works on Keys only)

d1 = {'fname':'Rahul', 1:'Dravid', 'dept':'CSE', 'mobile':9797979797}

print ('fname' in d1)
print ('CSE' in d1)   #CSE in not key, CSE is the value

Output

True
False

Looping over dictionary in python

for loop return the keys of the dictionary in each iteration. The value present at the key position can be access using an indexing operator that is []

#Looping over dictionary

months = { 'jan' : 1 , 'feb' : 2, 'march': 3, 'april': 4, 'may': 5}
for k in months:
    print(k, "-->", months[k])

Output

jan --> 1
feb --> 2
march --> 3
april --> 4
may --> 5

Summary:

This tutorial discusses the basic concepts of dictionaries in Python, How to use dictionaries in Python, in operator, looping over dictionaries. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

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