0% found this document useful (0 votes)
20 views17 pages

L8 Dictionaries

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views17 pages

L8 Dictionaries

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

ED5340 - Data Science: Theory at h y

and Practise a p
gan
th u
M u
a n
th
L8 - Dictionaries m a n a
R a

Ramanathan Muthuganapathy (https://ed.iitm.ac.in/~raman)


Course web page: https://ed.iitm.ac.in/~raman/datascience.html
Moodle page: Available at https://courses.iitm.ac.in/
Dictionaries
Intro

• Dictionary is a collection of key-value pair rep. as key : value


h y
at
• Enclosed in curly brackets { } an
a p
u g
uth
• Indexed only by keys. th a n M
n a
am
• Keys in a dictionary must a
be Runique and immutable.

• Strings / Tuples can be used as keys.


• Different keys may have same values!

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Dictionary representation

• Set is a collection of dissimilar data types using curly brackets { }


h y
at
• dc = {key : value} i.e. {key1 : value1, key2 :anvalue2,
ap ……….}
u g
uth
• E.g. dct1 = {10 : 100, 20 : 200, ‘ED1’
th a n : ‘name1’, ‘ED2’ : ‘name2’}
M
n a
m a
• dct2 = { } #Empty dictionary R a

• using dict.fromkeys( ) function.

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Accessing dictionary elements

• Using its key, the corresponding value can be accessed.


h y
at
• print the dictionary using its variable name.anap
u g
uth
• Iterated using the following ways: than M
n a
m a
• over key-value pairs. R a

• over keys.
• over values.

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


y

Demo using
at h
a p
gan
th u
u

L8_dict_ex_access.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


h y
at
CW: Define a dictionary and print all its a p
gan
th u
u

values using the corresponding keys.


n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Dictionary operations

• Dictionaries are ‘mutable’ (you can add, delete or modify elements).


h y
at
• Two dictionaries cannot be concatenated using
a na p + (instead, use update( )).
u g
uth
• searching (containment) and sorting
th a n- in, not in
M
n a
a
m
• conversion / other a
functions R- len, max, min, sum, sorted

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


y

Demo using
at h
a p
gan
th u
u

L8_dict_operations.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


HW: Define a dictionary of student namespand at h y find the max, min of the
a
dictionary. Arrange them in sorted order. u g a Print all of them clearly. Also
n
th
perform addition, deletion and modification n M u of a few of the entries.
th a
n a
a
What are you observations Ron max, min and sorting?
a m

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Dictionary methods - Member functions

• Given a dictionary D, you can apply the following member functions.


h y
at
• D.clear( ) - clears all the dictionary contents
an
a p
u g
uth
• D.update(D1) - adds D1 to D. th a n M
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


h y
HW: Define three dictionaries an
a p at
u g
and concatenate all
of them in another dictionary. h a n M u th
Clear the contents
of all of them. Check if the dictionaries are empty.
a t
a n
m
Ra

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Dictionary varieties

• Nested Dictonary (similar to nested structure)


h y
at
• Dictionary unpacking (using * operator) an
a p
u g
uth
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Nested dictionary

studlist = {‘Anand’ : {‘DOB’ : ’20/11/2001’, ‘Roll’ : ‘ED1234’ },


h y
‘Ramesh’ : {‘DOB’ : ’19/11/2001’, ‘Roll’ : ‘ED1235’p }, at
na
a g
th u
u
‘Kamesh’ : {‘DOB’ : ’21/11/2001’, ‘Roll’
h a n : ‘ED1236’ } }
M
a t
an
m
Ra
How do you print, access and change contents of nested dictionary?

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


y

Demo using
at h
a p
gan
th u
u

L8_dict_varieties.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Dictionary Comprehension
same as list comprehension except use curly brackets and key:value pairs

The syntax goes like this:


h y
dctA = {key:value for (key, value) in dict.items( at
p ) [optional for and/or if]}
a
an g
th u
u
where dict is another dictionary! a n M
ath
a n
a m
You can do with other than dict.items(
R ) as well. How?

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Demo using gan
a pat h y

L8_dict_comprehension.py th u
M u
a n
th
(CW, HW is available in that file itself)
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


CW: Create a dictionary consisting of gan
a pat h y

squares of integers from 1 to 9 using th u


M u
a n
th
dictionary comprehension n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras

You might also like