Home / Blog / Dictionaries in Python: A Comprehensive Tutorial
Dictionaries in Python

Dictionaries in Python: A Comprehensive Tutorial

Dictionaries in Python are a fundamental data structure that stores a collection of key-value pairs. Python dictionaries help to store objects’ unordered dictionaries. We define dictionaries using curly brackets; they are a pair of keys and a value. We can not retrieve a value through its index because dictionaries do not store values in order or sequence.

Code

dic = {'key': 'value1', 'key2': 2}
print(dic)
Python
Output: {‘key’: ‘value1’, ‘key2’: 2}

Note: We cannot retrieve a value through its index because dictionaries do not store values in order or sequence. We can retrieve a value by using its key.

Access Item from Dictionaries

Code

dic = {'key1': 'value1', 'key2': 2}
print(dic['key1'])
Python
Output: value1

We can also use get () which will give you the same result:

print (dic.get(key1))
Python
Output: value1

Change Item from Dictionary

We can change any item value from the dictionary by using its key name:

Code

dic = {'key1': 'value1', 'key2': 2}
dic["key2"] = 5
print(dic)
Python
Output:
{‘key1’: ‘value1’, ‘key2’: 5}

Add new Item

We can change any item value from the dictionary by using its key name:

Code

dic = {'key1': 'value1', 'key2': 2, 'key3': 'value3'}
dic["key3"] = "red"
print(dic)
Python
Output:
{‘key1’: ‘value1’, ‘key2’: 2, ‘key3’: ‘red’}

Print a List using for loop

We can print our dictionary by using the for loop:

Note: When we print dictionary values through looping only key values will be printed, there are methods to print values as well.

Code

dic = {'key1': 'value1', 'key2': 2}

for key in dic:
    print(dic[key])
Python
Output:
value1
2

Check if the Item exists or not:

Example

Keywords give us the facility to check whether a specified key is present in a dictionary or not:

Code

dic = {'key1': 'value1', 'key2': 2}

# Using the get() method to check if the key exists
if dic.get('key2') is not None:
    print("Yes, 'key2' exists in this dictionary")
Python
Output: Yes, ‘key2’ exists in this dictionary 

Print values one by one

We can also print values one by one:

Code

dic = {'key1': 'value1', 'key2': 2}

for x in dic:
    print(dic[x])
Python
Output:
value1              
2

Print Key and Value in Python

We can print keys and values both at the same time by using the item () function in Python.

Code

dic = {'key1': 'value1', 'key2': 2}

for x, y in dic.items():
    print(x, y)
Python
Output:
key1 value1
key2 2

Length of Dictionary

By using the len () method we can determine how many items exist in the dictionary:

Code

dic = {'key1': 'value1', 'key2': 2}
print(len(dic))
Python
Output:

Pop item from Dictionary

Pop Items in the dictionary by using popitem () method will remove the last item:

Code

dic = {'key1': 'value1', 'key2': 2}
dic.popitem()
print(dic)
Python
Output: {‘key1’: ‘value1’}

Delete item from Dictionary

We can delete any Items from a dictionary by using the del () method:

Code

dic = {‘key1’: ‘value1’, ‘key2’: 2}

del dic [“key2”]

print (dic)
Python
Output: {‘key1’: ‘value1’}

Clear items from the whole Dictionary

We can remove our dictionary Items by using clear () method:

Code

dic = {'key1': 'value1', 'key2': 2}
dic.clear()
print(dic)
Python
Output: { }

You can also read:

How to Install Python on Windows | Introduction to Python
A Beginner’s Guide To Python Variables and Types
Python Operators | Types of Operators in Python
Basic Python Syntax | First Program in Python
Python Arrays | List Methods

Dictionaries in Python: A Comprehensive Tutorial

Author: TCF Editorial
Copyright The Cloudflare.

Post navigation