Dictionaries
Reading
Chapter 12.
Key ideas
From chapter 12
- A dictionary is an unordered collection of key-value pairs.
- Creating an empty dictionary: homeruns = {}
- Adding a key-value pair: homeruns["Babe Ruth"] = 713(note: he hit 714)
- Adding another pair: homeruns["Mickey Mantle"] = 536
- Deleting an item: del homeruns["Mickey Mantle"]
- Modifying an item: homeruns["Babe Ruth"] = 714
- Determining the number of items in a dictionary: len(homeruns)
Keys
- To retrieve all keys in a dictionary named inventory: inventory.keys()
- To retrieve all keys as a list: list(inventory.keys())
- To retrieve keys one at a time: for key in inventory:
- To check if a key named “kiwi” exists: if "kiwi" in inventory
Values
- To retrieve all values: inventory.values()
- To retrieve a specific value, producing a runtime error if the key is not present: inventory[key-name]
- To retrieve a specific value, producing NO runtime error if the key is not present: inventory.get(key-name)orinventory.get(key-name, value-to-return-if-key-not-present)
Key-Value Pairs
- To retrieve all key-value pairs: inventory.items()
Aliasing vs. copying
- An example of an alias: dictionary1 = dictionary2
- The expression dictionary2 is dictionary1returnsTrue
- An example of a copy: dictionary2 = dictionary1.copy()
- The expression dictionary2 is dictionary1returnsFalse
- A dictionary handles sparse data well. For example, consider keeping track of how many times a word occurs in a novel such as The Catcher in the Rye. Only words that appear one or more times need to be stored.
Active learning
Activity 1
On paper, answer the following questions:
- What is printed by the following statements?mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} keylist = list(mydict.keys()) keylist.sort() print(keylist[3])
- What is printed by the following statements?mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} answer = mydict.get("cat") // mydict.get("dog") print(answer)
- What is printed by the following statements?mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} print("dog" in mydict)
- What is printed by the following statements?mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} print(23 in mydict)
- What is printed by the following statements?total = 0 mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} for akey in mydict: if len(akey) > 3: total = total + mydict[akey] print(total)
- What is printed by the following?mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} yourdict = mydict yourdict["elephant"] = 999 print(mydict["elephant"])
Activity 2
Download raven.py and raven.txt. Edit raven.py to find the counts of every letter in the text using a dictionary.
Activity 3
Download raven_words.py and
- Modify raven_words.py to produce a file word_counts.txt that contains the counts of each word in the file raven.txt, in alphabetical order. The first few lines of word_counts.txt should look something like this:a 15 above 7 adore 1 again 1 agreeing 1 ah 2 aidenn 1 air 1 all 4 an 2 ancient 1 and 38 angels 4 answer 1Hint: you may need to cast a dict_keys object to a list using list()Here is a solution: raven_words_updated.py.
- Modify your program to also print the longest word in the file.