CodingNic

Python Dictionaries

Dictionary Exercises

Python Dictionaries 25 min read

Dictionary Exercises

Objectives

This chapter introduces no new concepts. It’s a chance to put dictionaries, tuples, sets, and dictionary comprehension together.

Part I: Dictionary Comprehension

Complete each of these using dictionary comprehension.

  1. Given the list [("name", "Erin"), ("job", "Instructor")], create a dictionary that looks like {'job': 'Instructor', 'name': 'Erin'} (order doesn’t matter).
  2. Given the lists ["CA", "NJ", "RI"] and ["California", "New Jersey", "Rhode Island"], return a dictionary that looks like {'CA': 'California', 'NJ': 'New Jersey', 'RI': 'Rhode Island'}. Look into the zip function to help with this one.
  3. Create a dictionary with each vowel as a key and 0 as the value. It should look like {'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}. (Don’t use .fromkeys().)
  4. Create a dictionary mapping each letter’s position in the alphabet to the letter itself (hint: chr(65) gives you the first letter). It should look like:
text
{1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I',
 10: 'J', 11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'Q',
 18: 'R', 19: 'S', 20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y', 26: 'Z'}
  1. Given {"a": 1, "b": 2, "c": 3}, return a new dictionary with the same keys but every value doubled ({'a': 2, 'b': 4, 'c': 6}).
  2. Given a sentence, build a dictionary mapping each unique word to its length. word_lengths("the quick brown fox") should return {'the': 3, 'quick': 5, 'brown': 5, 'fox': 3}.
  3. Given {"a": 1, "b": 2, "c": 3, "d": 4}, return a new dictionary containing only the pairs whose value is even ({'b': 2, 'd': 4}).
  4. Given a dictionary of student names to lists of test scores, build a new dictionary mapping each name to their average score, rounded to one decimal place.
python
scores = {"Erin": [90, 85, 92], "Jordan": [70, 75, 80]}
average_scores(scores)  # {'Erin': 89.0, 'Jordan': 75.0}

Part II: Tuples and Sets

  1. Write a function coordinates_from_string(s) that takes a string like "3,4" and returns it as a tuple of integers, (3, 4).
  2. You’re given two lists of names: everyone who attended a conference on day one, and everyone who attended on day two:
python
day_one = ["Erin", "Jordan", "Maya", "Priya"]
day_two = ["Maya", "Priya", "Sam", "Jordan Reyes"]

Convert both to sets, then use set methods to find: everyone who attended both days (.intersection(), should be {'Maya', 'Priya'}), everyone who attended only day one (.difference(), should be {'Erin', 'Jordan'}), and everyone who attended at least one day (.union(), should contain all six names).
11. Write a function has_duplicates(items) that returns True if a list contains any repeated values and False otherwise. (Hint: compare the list’s length to the length of a set built from it.)
12. Given a tuple representing an RGB color, like (255, 0, 0), write a function that returns True if all three values are valid (between 0 and 255 inclusive), and False otherwise.

Recap

You can now create, read, and modify dictionaries, loop over them, use dictionary comprehension to build them from other data, and reach for a tuple or a set when that’s a better fit than a list or dictionary. That’s the toolkit Module 3 set out to build.

Next module: functions, and how to package up code so you can reuse it.