SlideShare a Scribd company logo
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
C HA PT E R 9
Dictionaries
and Sets
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Topics
Dictionaries
Sets
Serializing Objects
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Dictionaries
Dictionary: object that stores a
collection of data
Each element consists of a key and a value
Often referred to as mapping of key to value
Key must be an immutable object
To retrieve a specific value, use the key
associated with it
Format for creating a dictionary
dictionary =
{key1:val1, key2:val2}
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Retrieving a Value from a
Dictionary
Elements in dictionary are unsorted
General format for retrieving value from
dictionary: dictionary[key]
If key in the dictionary, associated value is
returned, otherwise, KeyError exception is
raised
Test whether a key is in a dictionary
using the in and not in operators
Helps prevent KeyError exceptions
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Adding Elements to an
Existing Dictionary
Dictionaries are mutable objects
To add a new key-value pair:
dictionary[key] = value
If key exists in the dictionary, the value
associated with it will be changed
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Deleting Elements From an
Existing Dictionary
To delete a key-value pair:
del dictionary[key]
If key is not in the dictionary, KeyError
exception is raised
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Getting the Number of Elements
and Mixing Data Types
len function: used to obtain number of
elements in a dictionary
Keys must be immutable objects, but
associated values can be any type of
object
One dictionary can include keys of several
different immutable types
Values stored in a single dictionary can
be of different types
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Creating an Empty Dictionary and
Using for Loop to Iterate Over a
Dictionary
To create an empty dictionary:
Use {}
Use built-in function dict()
Elements can be added to the dictionary as
program executes
Use a for loop to iterate over a
dictionary
General format: for key in dictionary:
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Some Dictionary Methods
clear method: deletes all the elements
in a dictionary, leaving it empty
Format: dictionary.clear()
get method: gets a value associated
with specified key from the dictionary
Format: dictionary.get(key, default)
default is returned if key is not found
Alternative to [] operator
Cannot raise KeyError exception
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
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
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Some Dictionary Methods
(cont’d.)
keys method: returns all the
dictionaries keys as a sequence
Format: dictionary.keys()
pop method: returns value associated
with specified key and removes that
key-value pair from the dictionary
Format: dictionary.pop(key, default)
default is returned if key is not found
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Some Dictionary Methods
(cont’d.)
popitem method: returns a randomly
selected key-value pair and removes
that key-value pair from the dictionary
Format: dictionary.popitem()
Key-value pair returned as a tuple
values method: returns all the
dictionaries values as a sequence
Format: dictionary.values()
Use a for loop to iterate over the values
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Some Dictionary Methods
(cont’d.)
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Sets
Set: object that stores a collection of
data in same way as mathematical set
All items must be unique
Set is unordered
Elements can be of different data types
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Creating a Set
set function: used to create a set
For empty set, call set()
For non-empty set, call set(argument)
where argument is an object that contains
iterable elements
e.g., argument can be a list, string, or tuple
If argument is a string, each character becomes a
set element
For set of strings, pass them to the function as a list
If argument contains duplicates, only one of the
duplicates will appear in the set
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Getting the Number of and
Adding Elements
len function: returns the number of
elements in the set
Sets are mutable objects
add method: adds an element to a set
update method: adds a group of
elements to a set
Argument must be a sequence containing
iterable elements, and each of the elements is
added to the set
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Deleting Elements From a Set
remove and discard methods: remove
the specified item from the set
The item that should be removed is passed to
both methods as an argument
Behave differently when the specified item is
not found in the set
remove method raises a KeyError exception
discard method does not raise an exception
clear method: clears all the elements
of the set
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Using the for Loop, in, and
not in Operators With a Set
A for loop can be used to iterate over
elements in a set
General format: for item in set:
The loop iterates once for each element in the
set
The in operator can be used to test
whether a value exists in a set
Similarly, the not in operator can be used to
test whether a value does not exist in a set
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Finding the Union of Sets
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
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Finding the Intersection of
Sets
Intersection of two sets: a set that
contains only the elements found in
both sets
To find the intersection of two sets:
Use the intersection method
Format: set1.intersection(set2)
Use the & operator
Format: set1 & set2
Both techniques return a new set which
contains the intersection of both sets
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Finding the Difference of Sets
Difference of two sets: a set that
contains the elements that appear in
the first set but do not appear in the
second set
To find the difference of two sets:
Use the difference method
Format: set1.difference(set2)
Use the - operator
Format: set1 - set2
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Finding the Symmetric
Difference of Sets
Symmetric difference of two sets: a set
that contains the elements that are not
shared by the two sets
To find the symmetric difference of two
sets:
Use the symmetric_difference method
Format: set1.symmetric_difference(set2)
Use the ^ operator
Format: set1 ^ set2
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Finding Subsets and
Supersets
Set A is subset of set B if all the
elements in set A are included in set B
To determine whether set A is subset
of set B
Use the issubset method
Format: setA.issubset(setB)
Use the <= operator
Format: setA <= setB
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Finding Subsets and
Supersets (cont’d.)
Set A is superset of set B if it contains
all the elements of set B
To determine whether set A is superset
of set B
Use the issuperset method
Format: setA.issuperset(setB)
Use the >= operator
Format: setA >= setB
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Serializing Objects
Serialize an object: convert the object
to a stream of bytes that can easily be
stored in a file
Pickling: serializing an object
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Serializing Objects (cont’d.)
To pickle an object:
Import the pickle module
Open a file for binary writing
Call the pickle.dump function
Format: pickle.dump(object, file)
Close the file
You can pickle multiple objects to one
file prior to closing the file
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Serializing Objects (cont’d.)
Unpickling: retrieving pickled object
To unpickle an object:
Import the pickle module
Open a file for binary writing
Call the pickle.load function
Format: pickle.load(file)
Close the file
You can unpickle multiple objects from
the file
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Summary
This chapter covered:
Dictionaries, including:
Creating dictionaries
Inserting, retrieving, adding, and deleting key-value
pairs
for loops and in and not in operators
Dictionary methods
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Summary (cont’d.)
This chapter covered (cont’d):
Sets:
Creating sets
Adding elements to and removing elements from
sets
Finding set union, intersection, difference and
symmetric difference
Finding subsets and supersets
Serializing objects
Pickling and unpickling objects

More Related Content

PDF
Python list
PDF
Python programming : Strings
PPTX
Dictionaries and Sets in Python
PPTX
Unit 4 python -list methods
PPSX
Data Structure (Queue)
PDF
Python libraries
PPTX
Data Structures in Python
PDF
Python set
Python list
Python programming : Strings
Dictionaries and Sets in Python
Unit 4 python -list methods
Data Structure (Queue)
Python libraries
Data Structures in Python
Python set

What's hot (20)

PDF
Datatypes in python
PPTX
Python dictionary
PPTX
Queue in Data Structure
PPSX
Modules and packages in python
PPTX
Python dictionary
PDF
Php array
PPTX
Introduction to stack
PDF
List,tuple,dictionary
ODP
Python Modules
PPTX
Dictionaries and Sets in Python
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
PPTX
Stack and Queue
PPS
Wrapper class
PPT
Asymptotic notation
PDF
Python programming : List and tuples
PPTX
Binary Search Tree
PPTX
Introduction to data structure ppt
PPTX
Introduction to numpy Session 1
PPT
Datatypes in python
Python dictionary
Queue in Data Structure
Modules and packages in python
Python dictionary
Php array
Introduction to stack
List,tuple,dictionary
Python Modules
Dictionaries and Sets in Python
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Stack and Queue
Wrapper class
Asymptotic notation
Python programming : List and tuples
Binary Search Tree
Introduction to data structure ppt
Introduction to numpy Session 1
Ad

Similar to Python Dictionaries and Sets (20)

PPTX
6Dictionaries and sets in pythonsss.pptx
PPTX
Python-Chapter 08 in a Python Programming Course.pptx
PPTX
SETS IN PYTHON-157755566677778⁵567886676.pptx
PPTX
Python Sets_Dictionary.pptx
PDF
Set methods in python
PPTX
Introduction to sets
PPTX
Python introduction data structures lists etc
PPTX
Introduction-to-Sets-in-Python (Computer).pptx
PPT
Tuples, sets and dictionaries-Programming in Python
PPTX
Dictionaries and Sets
PPTX
Week 10.pptx
PPT
Tuples, sets angsdfgsfdgdfgfdgd dictionaries 2.ppt
PPTX
Python_Sets(1).pptx
PPTX
Discrete Math Chapter 2: Basic Structures: Sets, Functions, Sequences, Sums, ...
PPTX
Seventh session
PPTX
7. tuples, set &amp; dictionary
PPT
Sets in the python programming language.ppt
PPTX
Python programming -Tuple and Set Data type
PPTX
Explore the foundational concepts of sets in discrete mathematics
6Dictionaries and sets in pythonsss.pptx
Python-Chapter 08 in a Python Programming Course.pptx
SETS IN PYTHON-157755566677778⁵567886676.pptx
Python Sets_Dictionary.pptx
Set methods in python
Introduction to sets
Python introduction data structures lists etc
Introduction-to-Sets-in-Python (Computer).pptx
Tuples, sets and dictionaries-Programming in Python
Dictionaries and Sets
Week 10.pptx
Tuples, sets angsdfgsfdgdfgfdgd dictionaries 2.ppt
Python_Sets(1).pptx
Discrete Math Chapter 2: Basic Structures: Sets, Functions, Sequences, Sums, ...
Seventh session
7. tuples, set &amp; dictionary
Sets in the python programming language.ppt
Python programming -Tuple and Set Data type
Explore the foundational concepts of sets in discrete mathematics
Ad

More from Nicole Ryan (20)

PPT
Testing and Improving Performance
PPT
Optimizing a website for search engines
PPT
Inheritance
PPT
Javascript programming using the document object model
PPT
Working with Video and Audio
PPT
Working with Images
PPT
Creating Visual Effects and Animation
PPT
Creating and Processing Web Forms
PPT
Organizing Content with Lists and Tables
PPT
Social media and your website
PPT
Working with Links
PPT
Formatting text with CSS
PPT
Laying Out Elements with CSS
PPT
Getting Started with CSS
PPT
Structure Web Content
PPT
Getting Started with your Website
PPTX
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
PPTX
Chapter 11: Object Oriented Programming Part 2
PPTX
Intro to Programming: Modularity
PPT
Programming Logic and Design: Arrays
Testing and Improving Performance
Optimizing a website for search engines
Inheritance
Javascript programming using the document object model
Working with Video and Audio
Working with Images
Creating Visual Effects and Animation
Creating and Processing Web Forms
Organizing Content with Lists and Tables
Social media and your website
Working with Links
Formatting text with CSS
Laying Out Elements with CSS
Getting Started with CSS
Structure Web Content
Getting Started with your Website
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 11: Object Oriented Programming Part 2
Intro to Programming: Modularity
Programming Logic and Design: Arrays

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Institutional Correction lecture only . . .
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Business Ethics Teaching Materials for college
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Complications of Minimal Access Surgery at WLH
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Insiders guide to clinical Medicine.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Final Presentation General Medicine 03-08-2024.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
RMMM.pdf make it easy to upload and study
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Institutional Correction lecture only . . .
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
TR - Agricultural Crops Production NC III.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Microbial disease of the cardiovascular and lymphatic systems
Business Ethics Teaching Materials for college
Module 4: Burden of Disease Tutorial Slides S2 2025
Complications of Minimal Access Surgery at WLH
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
VCE English Exam - Section C Student Revision Booklet
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Insiders guide to clinical Medicine.pdf

Python Dictionaries and Sets

  • 1. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C HA PT E R 9 Dictionaries and Sets
  • 2. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Topics Dictionaries Sets Serializing Objects
  • 3. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dictionaries Dictionary: object that stores a collection of data Each element consists of a key and a value Often referred to as mapping of key to value Key must be an immutable object To retrieve a specific value, use the key associated with it Format for creating a dictionary dictionary = {key1:val1, key2:val2}
  • 4. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Retrieving a Value from a Dictionary Elements in dictionary are unsorted General format for retrieving value from dictionary: dictionary[key] If key in the dictionary, associated value is returned, otherwise, KeyError exception is raised Test whether a key is in a dictionary using the in and not in operators Helps prevent KeyError exceptions
  • 5. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Adding Elements to an Existing Dictionary Dictionaries are mutable objects To add a new key-value pair: dictionary[key] = value If key exists in the dictionary, the value associated with it will be changed
  • 6. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Deleting Elements From an Existing Dictionary To delete a key-value pair: del dictionary[key] If key is not in the dictionary, KeyError exception is raised
  • 7. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Getting the Number of Elements and Mixing Data Types len function: used to obtain number of elements in a dictionary Keys must be immutable objects, but associated values can be any type of object One dictionary can include keys of several different immutable types Values stored in a single dictionary can be of different types
  • 8. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Creating an Empty Dictionary and Using for Loop to Iterate Over a Dictionary To create an empty dictionary: Use {} Use built-in function dict() Elements can be added to the dictionary as program executes Use a for loop to iterate over a dictionary General format: for key in dictionary:
  • 9. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Some Dictionary Methods clear method: deletes all the elements in a dictionary, leaving it empty Format: dictionary.clear() get method: gets a value associated with specified key from the dictionary Format: dictionary.get(key, default) default is returned if key is not found Alternative to [] operator Cannot raise KeyError exception
  • 10. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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
  • 11. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Some Dictionary Methods (cont’d.) keys method: returns all the dictionaries keys as a sequence Format: dictionary.keys() pop method: returns value associated with specified key and removes that key-value pair from the dictionary Format: dictionary.pop(key, default) default is returned if key is not found
  • 12. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Some Dictionary Methods (cont’d.) popitem method: returns a randomly selected key-value pair and removes that key-value pair from the dictionary Format: dictionary.popitem() Key-value pair returned as a tuple values method: returns all the dictionaries values as a sequence Format: dictionary.values() Use a for loop to iterate over the values
  • 13. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Some Dictionary Methods (cont’d.)
  • 14. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Sets Set: object that stores a collection of data in same way as mathematical set All items must be unique Set is unordered Elements can be of different data types
  • 15. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Creating a Set set function: used to create a set For empty set, call set() For non-empty set, call set(argument) where argument is an object that contains iterable elements e.g., argument can be a list, string, or tuple If argument is a string, each character becomes a set element For set of strings, pass them to the function as a list If argument contains duplicates, only one of the duplicates will appear in the set
  • 16. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Getting the Number of and Adding Elements len function: returns the number of elements in the set Sets are mutable objects add method: adds an element to a set update method: adds a group of elements to a set Argument must be a sequence containing iterable elements, and each of the elements is added to the set
  • 17. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Deleting Elements From a Set remove and discard methods: remove the specified item from the set The item that should be removed is passed to both methods as an argument Behave differently when the specified item is not found in the set remove method raises a KeyError exception discard method does not raise an exception clear method: clears all the elements of the set
  • 18. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using the for Loop, in, and not in Operators With a Set A for loop can be used to iterate over elements in a set General format: for item in set: The loop iterates once for each element in the set The in operator can be used to test whether a value exists in a set Similarly, the not in operator can be used to test whether a value does not exist in a set
  • 19. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Finding the Union of Sets 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
  • 20. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Finding the Intersection of Sets Intersection of two sets: a set that contains only the elements found in both sets To find the intersection of two sets: Use the intersection method Format: set1.intersection(set2) Use the & operator Format: set1 & set2 Both techniques return a new set which contains the intersection of both sets
  • 21. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Finding the Difference of Sets Difference of two sets: a set that contains the elements that appear in the first set but do not appear in the second set To find the difference of two sets: Use the difference method Format: set1.difference(set2) Use the - operator Format: set1 - set2
  • 22. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Finding the Symmetric Difference of Sets Symmetric difference of two sets: a set that contains the elements that are not shared by the two sets To find the symmetric difference of two sets: Use the symmetric_difference method Format: set1.symmetric_difference(set2) Use the ^ operator Format: set1 ^ set2
  • 23. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Finding Subsets and Supersets Set A is subset of set B if all the elements in set A are included in set B To determine whether set A is subset of set B Use the issubset method Format: setA.issubset(setB) Use the <= operator Format: setA <= setB
  • 24. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Finding Subsets and Supersets (cont’d.) Set A is superset of set B if it contains all the elements of set B To determine whether set A is superset of set B Use the issuperset method Format: setA.issuperset(setB) Use the >= operator Format: setA >= setB
  • 25. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Serializing Objects Serialize an object: convert the object to a stream of bytes that can easily be stored in a file Pickling: serializing an object
  • 26. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Serializing Objects (cont’d.) To pickle an object: Import the pickle module Open a file for binary writing Call the pickle.dump function Format: pickle.dump(object, file) Close the file You can pickle multiple objects to one file prior to closing the file
  • 27. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Serializing Objects (cont’d.) Unpickling: retrieving pickled object To unpickle an object: Import the pickle module Open a file for binary writing Call the pickle.load function Format: pickle.load(file) Close the file You can unpickle multiple objects from the file
  • 28. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Summary This chapter covered: Dictionaries, including: Creating dictionaries Inserting, retrieving, adding, and deleting key-value pairs for loops and in and not in operators Dictionary methods
  • 29. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Summary (cont’d.) This chapter covered (cont’d): Sets: Creating sets Adding elements to and removing elements from sets Finding set union, intersection, difference and symmetric difference Finding subsets and supersets Serializing objects Pickling and unpickling objects