0% found this document useful (0 votes)
26 views29 pages

PSP Lecture 15 Dictionary

The document provides an overview of dictionary operations and methods in Python, including key functions such as clear(), copy(), get(), items(), keys(), values(), pop(), popitem(), setdefault(), and update(). It also distinguishes between shallow and deep copies, explaining their behaviors and differences. Additionally, it introduces the concept of OrderedDict, which maintains the order of insertion for elements starting from Python 3.7.

Uploaded by

AARYAN GUPTA
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)
26 views29 pages

PSP Lecture 15 Dictionary

The document provides an overview of dictionary operations and methods in Python, including key functions such as clear(), copy(), get(), items(), keys(), values(), pop(), popitem(), setdefault(), and update(). It also distinguishes between shallow and deep copies, explaining their behaviors and differences. Additionally, it introduces the concept of OrderedDict, which maintains the order of insertion for elements starting from Python 3.7.

Uploaded by

AARYAN GUPTA
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

Problem Solving using Python

(ITFC0101)
Dictionary

Dr. Naveen Kumar Gupta

Assistant Professor
Department of Information Technology
Dr B R Ambedkar National Institute of Technology Jalandhar
1
Dictionary Operations and Methods

2
• Dictionary uses hashing internally
• Similar to hashmap in java
• Key is unique but value may be repeated

3
Dictionary Methods
Functions Name Descriptions

clear() Removes all items from the dictionary

copy() Returns a shallow copy of the dictionary

fromkeys() Creates a dictionary from the given sequence

get() Returns the value for the given key

items() Return the list with all dictionary keys with values

Returns a view object that displays a list of all the keys in the
keys()
dictionary in order of insertion
4
Dictionary Methods
Functions
Descriptions
Name
pop() Returns and removes the element with the given key
popitem() Returns and removes the key-value pair from the dictionary
Returns the value of a key if the key is in the dictionary else inserts
setdefault()
the key with a value to the dictionary
values() Updates the dictionary with the elements from another dictionary

update() Returns a list of all the values available in a given dictionary

del keyword Delete specific item. del d1[4]

len() Number of items in dictionary

5
Dictionary Methods
fromkeys(seq, val)

• Seq: sequence to be transformed into a dictionary //Keys

• Val: Initial values need to be assigned to keys


• Defaults 🡪 None

6
Dictionary Methods
fromkeys(seq, val)

• Creates a new dictionary from the given sequence


• Returns the dictionary with key mapped and specific value

7
Dictionary Methods
fromkeys 🡪 behavior with mutable objects

8
Dictionary Methods
Dictionary Creation: Difference between fromkeys & Comprehension

9
Dictionary Methods
clear()

• Remove all the elements (key-value pairs) from a dictionary

10
Dictionary Methods
copy()
• Shallow copy: An object is a copy that shares
same references as original object

• copy() method returns a shallow copy of


dictionary

11
Difference between Shallow copy and Deep copy

Shallow Copy Deep Copy

Shallow Copy stores the references of objects to


Deep copy stores copies of the object’s value.
the original memory address.

Shallow Copy reflects changes made to the Deep copy doesn’t reflect changes made to the
new/copied object in the original object. new/copied object in the original object.

Shallow Copy stores the copy of the original Deep copy stores the copy of the original object
object and points the references to the objects. and recursively copies the objects as well.

A shallow copy is faster. Deep copy is comparatively slower.

12
Dictionary Methods
get()

• Return the value for the given key if present in the dictionary
• If not present 🡪 return None

• With Default Parameter

13
Dictionary Methods
get(): Difference with square brackets

• KeyError for non-exist value

14
Dictionary Methods
items()

• Retrieves a view object containing a list of tuples


• Each tuple represents a key-value pair from the dictionary
• A convenient and efficient way to access both the keys and values of a dictionary
simultaneously

15
Dictionary Methods
keys()

• Returns a view object with dictionary keys


• Allowing efficient access and iteration

16
Dictionary Methods
values()

• Returns a view object containing all dictionary values


• Accessed and iterated efficiently

17
Dictionary Methods
pop()

• Removes and retrieves the value linked with a given key from a dictionary
• If key is not present 🡪 can set an optional default value to be returned
• del doesn’t return

18
Dictionary Methods
popitem()
• Eliminates and returns a (key, value) pair from the dictionary
• pop() method requires a key
• popitem() takes out a pair (last inserted element) without requiring a
key

• del doesn’t return

19
Dictionary Methods
setdefault()

• Returns the value of a key (if the key is in dictionary)


• Otherwise, it inserts a key with the default value to the dictionary

20
Dictionary Methods
setdefault()

• Returns the value of a key (if the key is in dictionary)


• Otherwise, it inserts a key with the default value to the dictionary

21
Dictionary Methods
update()

• Updates the key-value pairs of a dictionary


• using elements from another dictionary
• (or) an iterable of key-value pairs
• Can include new data or merge it with existing dictionary entries

22
Dictionary Methods
update()

• Updates the key-value


pairs of a dictionary

• using an iterable of
key-value pairs

• Can include new data or


merge it with existing
dictionary entries

23
Dictionary Methods
update()

• Include new data or


merge it with existing
dictionary entries

24
Dictionary Methods
Functions
Descriptions
Name

values() Updates the dictionary with the elements from another dictionary

pop() Returns and removes the element with the given key

popitem() Returns and removes the key-value pair from the dictionary

Returns the value of a key if the key is in the dictionary else inserts
setdefault()
the key with a value to the dictionary

update() Returns a list of all the values available in a given dictionary

25
= shallow copy Deep Copy
Applies on mutable collections: list, array, dictionary, set

• Shallow copy creates a new array


• Does not create new copies of the elements within the array
• Instead, it points to the same elements as the original array

• A deep copy, creates a completely independent copy of both the array and
its data.

• Does not share any data with the original array

26
Difference between Shallow copy and Deep copy

Shallow Copy Deep Copy

Shallow Copy stores the references of objects to


Deep copy stores copies of the object’s value.
the original memory address.

Shallow Copy reflects changes made to the Deep copy doesn’t reflect changes made to the
new/copied object in the original object. new/copied object in the original object.

Shallow Copy stores the copy of the original object Deep copy stores the copy of the original object
and points the references to the objects. and recursively copies the objects as well.

A shallow copy is faster. Deep copy is comparatively slower.

27
Ordered Dictionary
Special concept: Python 3.7 onwards

• Arranged in insertion order of element


• Part of collection module
• Syntax: [Link]()

• Regular dictionary methods works as usual

28
Thank You!

29

You might also like