0% found this document useful (0 votes)
21 views25 pages

9 Python Dictionaries

Uploaded by

tanongnichi15
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)
21 views25 pages

9 Python Dictionaries

Uploaded by

tanongnichi15
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/ 25

Python

Dictionaries
At the end of this lesson…

• Access Items
• Change Values
• Loop through a dictionary
• Check if key exists
• Dictionary Lengths
• Remove, Add, Copy
• Nested Dictionary
Dictionary

A dictionary is a collection which is unordered, changeable and


indexed. In Python dictionaries are written with curly brackets, and
they have keys and values.
• Create and print a dictionary:
Accessing Items

You can access the items of a dictionary by referring to its key name, inside
square brackets:
• Get the value of the "model" key:
Accessing Items

There is also a method called get() that will give you the same result:
Change Values

You can change the value of a specific item by referring to its key name:
• Change the "year" to 2018:
Loop Through a Dictionary

You can loop through a dictionary by using a for loop.


When looping through a dictionary, the return value are the keys of the
dictionary, but there are methods to return the values as well.
• Print all key names in the dictionary, one by one:
Example:

• Print all values in the dictionary, one by one:


Example:

• You can also use the values() method to return values of a


dictionary:
Example:

• Loop through both keys and values, by using


the items() method:
Check if Key Exists

To determine if a specified key is present in a dictionary use


the in keyword:
• Check if "model" is present in the dictionary:
Dictionary Length

To determine how many items (key-value pairs) a dictionary has,


use the len() function.
• Print the number of items in the dictionary:
Adding Items

Adding an item to the dictionary is done by using a new index key and
assigning a value to it:
Removing Items

There are several methods to remove items from a dictionary:


• The pop() method removes the item with the specified key
name:
Removing Items

• The popitem() method removes the last inserted item (in


versions before 3.7, a random item is removed instead):
Removing Items

• The del keyword removes the item with the specified key
name:
Removing Items

• The clear() method empties the dictionary:


Copy a Dictionary

You cannot copy a dictionary simply by typing dict2 =


dict1, because: dict2 will only be a reference to dict1,
and changes made in dict1 will automatically also be
made in dict2.
There are ways to make a copy, one way is to use the
built-in Dictionary method copy().
Example:

Make a copy of a dictionary with the copy() method:


Example:

Another way to make a copy is to use the built-in function dict().


Nested Dictionaries

A dictionary can also contain many dictionaries,


this is called nested dictionaries.

Create a dictionary that contain three dictionaries:


Example:

Or, if you want to nest three dictionaries that


already exists as dictionaries:
Create three dictionaries, then
create one dictionary that will
contain the other three dictionaries:
The dict() contructor

It is also possible to use the dict() constructor to make a new dictionary:

Another way to make a copy is to use the built-in function dict().


Dictionary Method

Python has a set of built-in methods that you can use on dictionaries.
THANK YOU!

You might also like