(L4)Programming with Python (Intermediate Level)
(L4)Programming with Python (Intermediate Level)
(Intermediate Level)
Lesson-4 Dictionary
purse['money'] = 12
purse['candy'] = 3
{'money': 12, 'tissues': 75, 'candy': 3}
purse['tissues'] = 75
print(purse)
purse['candy'] = purse['candy'] + 2
print(purse)
{'money': 12, 'tissues': 75, 'candy': 5}
To see whether something appears as a value in a dictionary,
We can use the method values, which returns the values as a type that can be
converted to a list.
vals = list(eng2sp.values())
How about keys()?
print(vals)
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys()
print(x)
y = car.values()
print(y)
Comparing Lists and Dictionaries
>>> lst = list()
>>> lst.append(21)
Dictionaries are like lists except >>> lst.append(183)
that they use keys instead of >>> print(lst)
numbers to look up values. >>> lst[0] = 23
>>> print(lst)
Lists index their entries based on
the position in the list.
>>> ddd = dict()
Dictionaries are like bags - no >>> ddd['age'] = 21
order. >>> ddd['course'] = 182
>>> print(ddd)
So we index the things we put in
>>> ddd['age'] = 23
the dictionary with a “lookup tag” >>> print(ddd)
Comparing Lists and Dictionaries
Do it yourself
1. Person:
Use a dictionary to store information about a person you know. Store their first
name, last name, age, and the city in which they live. You should have keys such
as first_name, last_name, age, and city. Print each piece of information stored in
your dictionary.
2. Favorite Numbers:
Use a dictionary to store people’s favorite numbers. Think of five names, and use
them as keys in your dictionary. Think of a favorite number for each person, and
store each as a value in your dictionary. Print each person’s name and their
favorite number.
Many Counters with a Dictionary
One common use of dictionaries is counting how often we “see” something
1. You could create 26 variables, one for each letter of the alphabet. Then you could traverse the string and, for
each character, increment the corresponding counter, probably using a chained conditional.
2. You could create a list with 26 elements. Then you could convert each character to a number (using the built-in
function ord), use the number as an index into the list, and increment the appropriate counter.
3. You could create a dictionary with characters as keys and counters as the corresponding values. The first time
you see a character, you would add an item to the dictionary. After that you would increment the value of an
existing item.
Each of these options performs the same computation, but each of them implements that computation in a different
way.
An implementation is a way of performing a computation; some implementations are better than others. For example,
an advantage of the dictionary implementation is that we don’t have to know ahead of time which letters appear in the
string and we only have to make room for the letters that do appear.
Exercise
1. Write a program to read through a mail log, build a histogram using a
dictionary to count how many messages have come from each email address,
and print the dictionary.
2. This program records the domain name (instead of the address) where the
message was sent from instead of who the mail came from (i.e., the whole
email address). At the end of the program, print out the contents of your
dictionary.
3. How to delete item from dictionary and how to handle the exception that
raises a KeyError if key is not in the map?
Hints→ https://docs.python.org/3.10/library/stdtypes.html#mapping-types-dict
Definite Loops and Dictionaries
Even though dictionaries are not stored in order, we can write a for loop that goes
through all the entries in a dictionary - actually it goes through all of the keys in the
dictionary and looks up the values
counts = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}
print(key, counts[key])
You can get a list of keys, values, or items (both) from a dictionary
print(list(jjj.items()))
A items() method is used with a dictionary to get the list with all dictionary keys
with values.
This method actually returns a view object that contains the key-value pairs of the
dictionary, as tuples in a list.
Bonus: Two Iteration Variables!
We loop through the key-value pairs in a dictionary using *two* iteration
variables
Each iteration, the first variable is the key and the second variable is the
corresponding value for the key