1/4/2021 Basics of Dictionary in Python - Jupyter Notebook
In [1]: # We have seen Array, List, Tuple, Set
# Some data types or user defined data types
# Arrangement of data in squence
# Ordered data
# Collections of data (Heterogeeous or Homogeneous)
# Dictionary in python
'''
Dictionary in python is an ordered collection of data values, used to store
data values like a map which unlike other data types that hold only single value
as an element.'''
#Dictionary hold key:value pair.
#Key value is provided in the dictionary to make it more optimized.
# List or tuple --- Heterogeneous data values
# Array --- Homogeneous data values
# But there is no guarantee that elements will be in ordered.
# Set is also kind of data type to hold ordered elements.
# Dictionary ordered collection of data type, KEY:VALUE
Out[1]: '\nDictionary in python is an ordered collection of data values, used to store
\ndata values like a map which unlike other data types that hold only single va
lue\nas an element.'
In [3]: # Creating Python dictionary
# key:value
# values can be of any data type (string, number or tuple with immutable elements
# and values must be defined unique
#empty dictionary
a = {}
print (a)
print (type(a))
b={}
b=set()
print (b)
print (type(b))
{}
<class 'dict'>
set()
<class 'set'>
localhost:8888/notebooks/CSE1021/Basics of Dictionary in Python.ipynb 1/5
1/4/2021 Basics of Dictionary in Python - Jupyter Notebook
In [7]: #SET
#c={1,2,3,4} or c=set()
#type(c)
#c={} #dictionary
#type(c)
#dictionary with integer keys
d = {1:'Apple', 2:'Ball'} #key:value
print (d)
{1: 'Apple', 2: 'Ball'}
In [8]: d = {1:'Apple', 'name':'Ball'}
print (d)
{1: 'Apple', 'name': 'Ball'}
In [9]: d = {1:'Apple', 'name':56}
print (d)
{1: 'Apple', 'name': 56}
In [14]: # Mixed types
d = {'name':'virendra', 1:90847, 56:[2,4,6,8,0]}
# 56 is key and its value is represented by a list
# key and value can be of any data type
# but key must be unique else by values it will be updated and count as unique
x = {'name':'virendra', 56:90847, 56:[2,4,6,8,0]}
#56:90847 will overwritten by 56:[2,4,6,8,0]
print (d)
print (x)
{'name': 'virendra', 1: 90847, 56: [2, 4, 6, 8, 0]}
{'name': 'virendra', 56: [2, 4, 6, 8, 0]}
In [15]: # using dict ()
x = dict ({'name':'virendra', 'age':56, 56:[12,34,76,8]})
print (x)
print (type(x))
{'name': 'virendra', 'age': 56, 56: [12, 34, 76, 8]}
<class 'dict'>
localhost:8888/notebooks/CSE1021/Basics of Dictionary in Python.ipynb 2/5
1/4/2021 Basics of Dictionary in Python - Jupyter Notebook
In [17]: # Accessing elements from dictionary
# get () method and other one is by index or [] to retrieve the elements
print (x['age']) #pass key and return value of the key #56
# get ()
print (x.get('age')) # 56
print (x.get(56)) # [12,34,76,8]
56
56
[12, 34, 76, 8]
In [19]: print (x.get('address')) #address key does not exist into x dictionary
#get() method return the NONE if key does exist into x dictionary
print (x['address'])
# it will return the error because by index passing
# we are tyring to access by forcefully to retrive the such key value
None
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-19-9f53ccf4c865> in <module>
1 print (x.get('address')) #address key does not exist into x dictionary
----> 2 print (x['address'])
KeyError: 'address'
In [20]: # Changing and Updating dictionary elements
z = {1:56, 2:68, 3:434, 4:656, 5:98, 6:1234, 7:9487}
print (z)
print (type(z))
{1: 56, 2: 68, 3: 434, 4: 656, 5: 98, 6: 1234, 7: 9487}
<class 'dict'>
In [21]: # update the value of the key = 4
z[4] = 500
print (z)
{1: 56, 2: 68, 3: 434, 4: 500, 5: 98, 6: 1234, 7: 9487}
In [22]: # Add an item
z['address'] = 'New Delhi'
# Z ditionary does not contain any key = 'address'
print (z)
{1: 56, 2: 68, 3: 434, 4: 500, 5: 98, 6: 1234, 7: 9487, 'address': 'New Delhi'}
localhost:8888/notebooks/CSE1021/Basics of Dictionary in Python.ipynb 3/5
1/4/2021 Basics of Dictionary in Python - Jupyter Notebook
In [23]: # If there is no existence of any key and we try to update or assign some value
# then it add automatically without affecting others key of the dictionary
z['age'] = 99
print (z)
{1: 56, 2: 68, 3: 434, 4: 500, 5: 98, 6: 1234, 7: 9487, 'address': 'New Delhi',
'age': 99}
In [24]: # in place age we want to use marks, age-->marks
z['abc'] = 959
print (z)
{1: 56, 2: 68, 3: 434, 4: 500, 5: 98, 6: 1234, 7: 9487, 'address': 'New Delhi',
'age': 99, 'abc': 959}
In [27]: #by using pop () method, it is also possible to update the keys
z['pqr'] = z.pop('abc')
# First it will assign the value of abc key to pqr key
# then remove abc key from the dictionary
print (z)
{1: 56, 2: 68, 3: 434, 4: 500, 5: 98, 6: 1234, 7: 9487, 'address': 'New Delhi',
'age': 99, 'pqr': 959}
In [29]: # Removing the elements from the dictionary
print (z)
#remove age key
z.pop('age')
print (z)
{1: 56, 2: 68, 3: 434, 4: 500, 5: 98, 6: 1234, 7: 9487, 'address': 'New Delhi',
'age': 99, 'pqr': 959}
{1: 56, 2: 68, 3: 434, 4: 500, 5: 98, 6: 1234, 7: 9487, 'address': 'New Delhi',
'pqr': 959}
In [30]: # You are specifying the name of the key to remove the item / element
# You want to remove last element without knowing the key
# How you can remove?
#use popitem() method
z.popitem()
print (z)
{1: 56, 2: 68, 3: 434, 4: 500, 5: 98, 6: 1234, 7: 9487, 'address': 'New Delhi'}
In [31]: # Remove all the elements
# use clear () method
z.clear ()
print (z)
{}
localhost:8888/notebooks/CSE1021/Basics of Dictionary in Python.ipynb 4/5
1/4/2021 Basics of Dictionary in Python - Jupyter Notebook
In [32]: print (z)
{}
In [33]: # It means there exist a dictionary i.e; z without any element
# I want to remove the dictionary
# use del keyword or statement
del z
# This will delete the z dictionary.
In [34]: print (z)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-34-79102c64614c> in <module>
----> 1 print (z)
NameError: name 'z' is not defined
In [ ]: # So far today what we have seen
# Dictinary
# Creating a dictionary
# define dictionary with mixed types
# access the dictionary (get () or [])
# change or update the value of an element in a dictionary
# add an item into the dictionary
# remove an element or all the elements pop(), popitem(), clear ()
# remove the dictionary
localhost:8888/notebooks/CSE1021/Basics of Dictionary in Python.ipynb 5/5