Editor

Combine Two Sources

Intermediate · Write the code · from Extracting Information from Data: Cleaning, Filtering and Metadata

From the lesson: Why combine data from different sources?

Often a single source does not contain the data needed to reach a conclusion. The sales log says how many drinks sold, but nothing about the weather. A weather website has each day's high temperature, but nothing about drinks.

Read this part of the lesson

A coding club has two records. sign_ups maps each student's name to their grade, from the sign-up form. attendance maps names to the number of meetings attended, from the teacher's log. Some students signed up and never came; some came but never filled in the form.

Write combine(sign_ups, attendance) that returns a list of (name, grade, meetings) tuples, one for each student who appears in both records, sorted by name.

combine({'Ava': 10, 'Ben': 9}, {'Ben': 4, 'Cy': 2})  ->  [('Ben', 9, 4)]
def combine(sign_ups, attendance):
# Keep only the names found in both records
pass

What Check runs

Your code runs first, then each of these lines, and 1 more that stay hidden. The output must match exactly.

RunsExpected output
print(combine({"Ava": 10, "Ben": 9}, {"Ben": 4, "Cy": 2}))[('Ben', 9, 4)]
print(combine({"Zoe": 11, "Ari": 9, "Mo": 10}, {"Mo": 6, "Zoe": 3, "Ari": 8}))[('Ari', 9, 8), ('Mo', 10, 6), ('Zoe', 11, 3)]
print(combine({"Ava": 10}, {"Cy": 2}))[]

More Data Filtering exercises

All Python exercises · 50 practice problems