Dictionaries Sets Unit3
Dictionaries Sets Unit3
Dictionarie
s and Sets
Dictionaries
Sets
Serializing Objects
Note: You cannot have duplicate keys in a dictionary. When you assign a
value to an existing key, the new value replaces the existing value.
Pg no 395
M.Trupthi, Assistant Professor, Dept. of IT, CBIT, Hyderabad.
Some Dictionary Methods (cont’d.)
items method: returns all the dictionaries keys and
associated values
Format: dictionary.items()
Returned as a dictionary view
Each element in dictionary view is a tuple which
contains a key and its associated value
Use a for loop to iterate over the tuples in the
sequence
Can use a variable which receives a tuple, or can use
two variables which receive key and value
Union of two sets: a set that contains all the elements of both
sets
To find the union of two sets:
Use the union method
Format: set1.union(set2)
Use the | operator
Format: set1 | set2
Both techniques return a new set which contains the union of
both sets
set1 = set([1, 2, 3, 4]) set2 = set([3, 4, 5, 6])
set3 = set1 | set2 set3
O/P :{1, 2, 3, 4,
M.Trupthi, Assistant Professor,5, 6}of IT, CBIT, Hyderabad.
Dept.
Finding the Intersection of Sets
To pickle an object:
Import the pickle module
Open a file for binary writing
Call the pickle.dump function and write it to the specified file.
Format: pickle.dump(object, file)
Close the file
You can pickle multiple objects to one file prior to closin the file