6.100L | Fall 2022 | Undergraduate

Introduction to CS and Programming using Python

Lecture 14: Dictionaries

Topics: Dictionaries: keys, values, mutability, iteration over a dict, examples

Lecture Notes

Lecture 14: Dictionaries

Lecture 14 Code

Readings

Ch 5.7

Finger Exercise Lecture 14

Question 1: Implement the function that meets the specifications below:

def keys_with_value(aDict, target):
    """
    aDict: a dictionary
    target: an integer or string
    Assume that keys and values in aDict are integers or strings.
    Returns a sorted list of the keys in aDict with the value target.
    If aDict does not contain the value target, returns an empty list.
    """
    # Your code here  

# Examples:
aDict = {1:2, 2:4, 5:2}
target = 2   
print(keys_with_value(aDict, target)) # prints the list [1,5]

Question 2: Implement the function that meets the specifications below:

def all_positive(d):
    """
    d is a dictionary that maps int:list
    Suppose an element in d is a key k mapping to value v (a non-empty list).
    Returns the sorted list of all k whose v elements sums up to a 
    positive value.
    """
    # Your code here  

# Examples:
d = {5:[2,-4], 2:[1,2,3], 1:[2]}
print(all_positive(d))   # prints the list [1, 2]

6.100L Finger Exercises Lecture 14 Solutions

Recitation 7

Recitations were smaller sections that reviewed the previous lectures. Files contain recitation notes and worked examples in code.

Recitation 7 Notes and Code

Course Info

Instructor
As Taught In
Fall 2022
Learning Resource Types
Lecture Videos
Lecture Notes
Problem Sets
Programming Assignments with Examples
Readings
Recitation Notes