Sets – Operations

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Sets – Operations

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What is a list of dictionaries?
A A list where each item is a dictionary
B A dictionary of lists
2. people = [{'name':'Al'}]\npeople[0]['name'] returns?
A {'name':'Al'}
B 'Al'
3. d = {'items': [1,2,3]}\nd['items'][0] returns?
A 1
B [1, 2, 3]
4. Why combine lists and dicts?
A To store structured records
B For faster code
5. How do you add a dict to a list of dicts?
A .add({'key': 'val'})
B .append({'key': 'val'})

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1people = [{'name':'Ali'}] print(people['name']) AError: people is a list, use [0]
2d = {'tags': ['a','b']} print(d['tags']['a']) BOk: Append to list inside dict
3people = [{'name':'Ali'},{'name':'Bo'}] people[0]['name'] CError: Use integer index for list
4d = {'nums': [1,2,3]} d['nums'].append(4) DOutput: Ali Bo
5p = [{'n':'Ali'},{'n':'Bo'}] for x in p: print(x['n']) EValid: Returns 'Ali'
6d = {'c':['r','g']} print(d['c'][1]) FOutput: g

Definitions

List of dicts

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
people = [{'name': 'Alice'}] print(people[_]['name'])
2.
d = {'items': [1, 2, 3]} print(d['items'][_])
3.
people = [] people.______( {'name': 'Bob'} )
4.
for person __ people: print(person['name'])
5.
student = {'scores': [90, 85]} student['scores'].______(95)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A people = [{'name': 'Alice'}]
B greeting = people['name']
C print('Hello,', greeting)
D print('Done')
Question 2
A people = [{'name': 'Alice'}]
B greeting = people[0][0]
C print('Hello,', greeting)
D print('Done')
Question 3
A d = {'items': [1, 2]}
B d['items'].add(3)
C print('Items:', d['items'])
D print('Done')
Question 4
A people = {'name': 'Alice'}, {'name': 'Bob'}
B first = people[0]['name']
C print('First:', first)
D print('Count:', len(people))
Question 5
A d = {'scores': [90, 85]}
B print(d.scores[0])

Answer Key

Sets – Operations
Section A: Quick Questions
Q1
A — A list where each item is a dictionary
Q2
B — 'Al'
Q3
A — 1
Q4
A — To store structured records
Q5
B — .append({'key': 'val'})
Section B: Match the Pairs
Q1
people = [{'name':'Ali'}] print(people['name']) → Error: people is a list, use [0]
Q2
d = {'tags': ['a','b']} print(d['tags']['a']) → Error: Use integer index for list
Q3
people = [{'name':'Ali'},{'name':'Bo'}] people[0]['name'] → Valid: Returns 'Ali'
Q4
d = {'nums': [1,2,3]} d['nums'].append(4) → Ok: Append to list inside dict
Q5
p = [{'n':'Ali'},{'n':'Bo'}] for x in p: print(x['n']) → Output: Ali Bo
Q6
d = {'c':['r','g']} print(d['c'][1]) → Output: g
Section C: Definitions
Q1
List of dicts — A list where each element is a dictionary like records.~~list[i]['key']
Section D: Fill in the Blanks
Q1
people = [{'name': 'Alice'}] print(people[0]['name'])
Q2
d = {'items': [1, 2, 3]} print(d['items'][0])
Q3
people = [] people.append( {'name': 'Bob'} )
Q4
for person in people: print(person['name'])
Q5
student = {'scores': [90, 85]} student['scores'].append(95)
Section E: Spot the Error
Q1
B — people is a list: must index first: people[0]['name']
Q2
B — Dicts use keys not indices. Use ['name'] not [0].
Q3
B — Lists use .append(), not .add() (that's for sets)
Q4
A — Missing square brackets: this creates a tuple, not a list
Q5
B — Use bracket notation d['scores'], not dot notation d.scores