Dictionaries, Part 2

Intermediate Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

Recap

world = {"afghanistan":30.55, "albania":2.77, "algeria":39.21}

world["albania"]
2.77
world = {"afghanistan":30.55, "albania":2.77, 
         "algeria":39.21, "albania":2.81}

world
{'afghanistan': 30.55, 'albania': 2.81, 'algeria': 39.21}
Intermediate Python

Recap

  • Keys have to be "immutable" objects
{0:"hello", True:"dear", "two":"world"}
{0: 'hello', True: 'dear', 'two': 'world'}
{["just", "to", "test"]: "value"}
TypeError: unhashable type: 'list'
Intermediate Python

Principality of Sealand

ch2_2_slides.015.png

1 Source: Wikipedia
Intermediate Python

Dictionary

world["sealand"] = 0.000027

world
{'afghanistan': 30.55, 'albania': 2.81, 
        'algeria': 39.21, 'sealand': 2.7e-05}
"sealand" in world
True
Intermediate Python

Dictionary

world["sealand"] = 0.000028

world
{'afghanistan': 30.55, 'albania': 2.81, 
        'algeria': 39.21, 'sealand': 2.8e-05}
del(world["sealand"])

world
{'afghanistan': 30.55, 'albania': 2.81, 'algeria': 39.21}
Intermediate Python

List vs. Dictionary

Intermediate Python

List vs. Dictionary

Intermediate Python

List vs. Dictionary

List Dictionary
Select, update, and remove with [] Select, update, and remove with []
Intermediate Python

List vs. Dictionary

List Dictionary
Select, update, and remove with [] Select, update, and remove with []
Intermediate Python

List vs. Dictionary

List Dictionary
Select, update, and remove with [] Select, update, and remove with []
Indexed by range of numbers
Intermediate Python

List vs. Dictionary

List Dictionary
Select, update, and remove with [] Select, update, and remove with []
Indexed by range of numbers Indexed by unique keys
Intermediate Python

List vs. Dictionary

List Dictionary
Select, update, and remove with [] Select, update, and remove with []
Indexed by range of numbers Indexed by unique keys
Collection of values — order matters, for selecting entire subsets
Intermediate Python

List vs. Dictionary

List Dictionary
Select, update, and remove with [] Select, update, and remove with []
Indexed by range of numbers Indexed by unique keys
Collection of values — order matters, for selecting entire subsets Lookup table with unique keys
Intermediate Python

Let's practice!

Intermediate Python

Preparing Video For Download...