Module 3 Python
CHAPTER 5: DICTIONARIES AND STRUCTURING DATA
1. The Dictionary Data Type
2. Pretty Printing
3. Using Data Structures to Model Real-World Things.
The Dictionary Data Type
A dictionary is a collection of many values. Indexes for dictionaries can use many different data
types, not just integers. Indexes for dictionaries are called keys, and a key with its associated value
is called a key-value pair.
A dictionary is typed with braces, {}.
This assigns a dictionary to the myCat variable. This dictionary’s keys are 'size', 'color', and
'disposition'. The values for these keys are 'fat', 'gray', and 'loud', respectively. You can access these
values through their keys:
Dictionaries can still use integer values as keys, but they do not have to start at 0 and can be any
number.
, Module 3 Python
Dictionaries vs. Lists
Unlike lists, items in dictionaries are unordered.
The first item in a list named spam would be spam[0]. But there is no “first” item in a dictionary.
While the order of items matters for determining whether two lists are the same, it does not matter
in what order the key-value pairs are typed in a dictionary.
Trying to access a key that does not exist in a dictionary will result in a KeyError error message,
much like a list’s “out-of-range” IndexError error message.
We can have arbitrary values for the keys that allows us to organize our data in powerful ways.
Ex: we want to store data about our friends’ birthdays. We can use a dictionary with the names as
keys and the birthdays as values.
Program Output
We create an initial dictionary and store it in birthdays 1.
We can see if the entered name exists as a key in the dictionary with the in keyword 2.
If the name is in the dictionary, we access the associated value using square brackets 3; if not, we
can add it using the same square bracket syntax combined with the assignment operator 4.
, Module 3 Python
The keys(), values(), and items() Methods
There are three dictionary methods that will return list-like values of the dictionary’s keys, values,
or both keys and values: keys(), values(), and items().
Data types (dict_keys, dict_values, and dict_items, respectively) can be used in for loops.
A for loop can iterate over the keys, values, or key-value pairs in a dictionary by using keys(),
values(), and items() methods.
The values in the dict_items value returned by the items() method are tuples of the key and value.
If we want a true list from one of these methods, pass its list-like return value to the list() function.
The list(spam.keys()) line takes the dict_keys value returned from keys() and passes it to list(),
which then returns a list value of ['color', 'age'].
We can also use the multiple assignment trick in a for loop to assign the key and value to separate
variables.
CHAPTER 5: DICTIONARIES AND STRUCTURING DATA
1. The Dictionary Data Type
2. Pretty Printing
3. Using Data Structures to Model Real-World Things.
The Dictionary Data Type
A dictionary is a collection of many values. Indexes for dictionaries can use many different data
types, not just integers. Indexes for dictionaries are called keys, and a key with its associated value
is called a key-value pair.
A dictionary is typed with braces, {}.
This assigns a dictionary to the myCat variable. This dictionary’s keys are 'size', 'color', and
'disposition'. The values for these keys are 'fat', 'gray', and 'loud', respectively. You can access these
values through their keys:
Dictionaries can still use integer values as keys, but they do not have to start at 0 and can be any
number.
, Module 3 Python
Dictionaries vs. Lists
Unlike lists, items in dictionaries are unordered.
The first item in a list named spam would be spam[0]. But there is no “first” item in a dictionary.
While the order of items matters for determining whether two lists are the same, it does not matter
in what order the key-value pairs are typed in a dictionary.
Trying to access a key that does not exist in a dictionary will result in a KeyError error message,
much like a list’s “out-of-range” IndexError error message.
We can have arbitrary values for the keys that allows us to organize our data in powerful ways.
Ex: we want to store data about our friends’ birthdays. We can use a dictionary with the names as
keys and the birthdays as values.
Program Output
We create an initial dictionary and store it in birthdays 1.
We can see if the entered name exists as a key in the dictionary with the in keyword 2.
If the name is in the dictionary, we access the associated value using square brackets 3; if not, we
can add it using the same square bracket syntax combined with the assignment operator 4.
, Module 3 Python
The keys(), values(), and items() Methods
There are three dictionary methods that will return list-like values of the dictionary’s keys, values,
or both keys and values: keys(), values(), and items().
Data types (dict_keys, dict_values, and dict_items, respectively) can be used in for loops.
A for loop can iterate over the keys, values, or key-value pairs in a dictionary by using keys(),
values(), and items() methods.
The values in the dict_items value returned by the items() method are tuples of the key and value.
If we want a true list from one of these methods, pass its list-like return value to the list() function.
The list(spam.keys()) line takes the dict_keys value returned from keys() and passes it to list(),
which then returns a list value of ['color', 'age'].
We can also use the multiple assignment trick in a for loop to assign the key and value to separate
variables.