18.python Dictionary
18.python Dictionary
Home /
Tutorial /
Python Dictionary
Python Dictionary
By Manoj 4.7 K Views 24 min read Updated on March 9, 2022
In this module of the Python tutorial you will learn in detail about Dictionary datatype in Python. You will learn how to create
Dictionary and how to access elements in the dictionary. This module also highlights various operations and various methods in
dictionary.
Python Tutorial
Numbers in Python
String in Python
Python Lists
Tuple in Python
Python Sets
Python Dictionary
Python Operators
Type conversion in
Python
Python If Else
Statements
Python Functions -
Define & Call a Functions
in Python
Lambda Function in
Python
Python Built in
Functions with Examples
Python Arrays
Python Modules
Python Dates
Python JSON
Python RegEx
PIP Python
Exception Handling in
Python
Enumerate Function in
Python
Python Queue
Python Dictionary
Python dictionary is yet another unordered collection of elements. The difference between Python dictionary and other
unordered Python data types such sets lies in the fact that, unlike sets, a dictionary contains keys and values rather than
just elements.
Like lists, Python dictionaries can also be changed and modified, but unlike Python lists the values in dictionaries are
accessed using keys and not by their positions. All the keys in a dictionary are mapped to their respective values. The value
can be of any data type in Python.
Learn more about Python from this Python Data Science Course to get ahead in your career!
We are going to learn all that we need to know about the dictionary data type in Python in order to get started with it.
dict1 ={“Brand”:”gucci”,”Industry”:”fashion”,”year”:1921}
print (dict1)
Output:
dict2 = {}
We can also create a dictionary by using an in-built method dict () as shown in the following example:
1. Using the key inside square brackets like we used to use the index inside square brackets.
Example:
dict1 = {“Brand”:”gucci”,”Industry”:”fashion”,”year”:1921}
print(dict1[‘year’])
Output:
1921
2. Using the get() method and passing the key as a parameter inside this method.
Example:
dict1 = {“Brand”:”gucci”,”Industry”:”fashion”,”year”:1921}
print (dict1.get(‘year’))
Output:
1921
for i in cubes:
print(cubes[i])
Output:
21
64
125
Example:
dict1 = {“Brand”:”gucci”,”Industry”:”fashion”,”year”:1921}
print(dict1)
print(dict1)
print(dict2)
Output:
{‘Brand’: ‘gucci’, ‘Industry’: ‘Fashion and Luxury’, ‘year’: 1921, ‘product’: ‘Tote Handbag’}
Remove Items from a Dictionary and Delete the Whole Dictionary in Python
There are various ways in which we can remove elements from a dictionary such as using the pop() method, the popitem()
method, or using the del keyword. Let’s understand all of these individually with the help of examples.
print(cubes.pop(4))
print(cubes)
Output:
64
print(cubes.popitem())
print(cubes)
Output:
(5, 125)
Kick-start your career in Python with the perfect Python Course in New York now!
del cubes[5]
print (cubes)
Output:
cubes.clear()
print(cubes)
Output:
{}
As discussed above, curly braces with nothing inside represent an empty dictionary. Since the clear() method deletes all
elements at once, the output of printing the dictionary after using the clear() method on it is an empty dictionary, that is, {}.
cubes.del()
print(cubes)
Output:
dict1 = {“Brand”:”gucci”,”Industry”:”fashion”,”year”:1921}
print(len(dict1))
Output:
Go for the most professional Python Course Online in Toronto for a stellar career now!
dict1 = {“Brand”:”gucci”,”Industry”:”fashion”,”year”:1921}
if “industry” in dict1:
Output:
for i in s_order:
print(i[0],i[1])
croissants 83
tea 67
biscuit 51
coffee 43
Update Dictionary
Python has an update() method that can insert specified items into a dictionary. The specified item can be a dictionary
itself, or iterable objects with key-value pairs.
car.update({“colour”: “Black”})
print(car)
print(employees[1]['name'])
print(employees[1]['age'])
print(employees[1]['sex'])
Jack
28
Male
print("Here is a Dict:")
d = {}
d['a'] = 1
d['b'] = 2
d['c'] = 3
d['d'] = 4
print(key, value)
od = OrderedDict()
od['a'] = 1
od['b'] = 2
od['c'] = 3
od['d'] = 4
print(key, value)
Here is a Dict:
a 1
c 3
b 2
d 4
a 1
b 2
c 3
d 4
keys = ['a','b','c','d','e']
values = [1,2,3,4,5]
print (dict1)
print(student_dictionary)
Method Description
With this, we come to the end of this module in Python Tutorial. Here, we talked about how to create a dictionary in Python,
how to access Items, perform operations in the dictionary in Python, looping Through a dictionary, Adding Items to a
dictionary, Removing Items from a dictionary, and delete the whole dictionary, Python dictionary length, checking all keys in
a dictionary in Python, and also discussed some common Python Dictionary Methods. Now, if you are interested in knowing