0% found this document useful (0 votes)
274 views14 pages

09 - Gaddis Python - Lecture - PPT - ch09

Uploaded by

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

09 - Gaddis Python - Lecture - PPT - ch09

Uploaded by

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

Starting out with Python

Fifth Edition

Chapter 9
Dictionaries

Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved 9-1
Topics
• Dictionaries

Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved 9-2
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
 Keys can be strings, integers, floating pt numbers, tuples
but no lists
– To retrieve a specific value, use the key associated
with it
– Format for creating a dictionary
dictionary =
{key1:val1, key2:val2}
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved 9-3
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 © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved 9-4
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 © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved 9-5
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 © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved 9-6
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 © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved 9-7
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 © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved 9-8
Some Dictionary Methods (1 of 5)
• 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 © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved 9-9
Some Dictionary Methods (2 of 5)
• 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 © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved 9 - 10
Some Dictionary Methods (3 of 5)
• 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 © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved 9 - 11
Some Dictionary Methods (4 of 5)
• popitem method: Returns, as a tuple, the key-value
pair that was last added to the dictionary. The method
also removes the 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 © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved 9 - 12
Some Dictionary Methods (5 of 5)
Table 9-1 Some of the dictionary methods

Method Description
Clear Clears the contents of a dictionary.
get Gets the value associated with a specified key. If the key is not found, the method
does not raise an exception. Instead, it returns a default value.
items Returns all the keys in a dictionary and their associated values as a sequence of
tuples.
keys Returns all the keys in a dictionary as a sequence of tuples.
pop Returns the value associated with a specified key and removes that key-value pair
from the dictionary. If the key is not found, the method returns a default value.
popitem Returns, as a tuple, the key-value pair that was last added to the dictionary. The
method also removes the key-value pair from the dictionary.
values Returns all the values in the dictionary as a sequence of tuples.

Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved 9 - 13
Summary (1 of 2)
• 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 © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved 9 - 14

You might also like