Nested Lists

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

Nested Lists

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What makes a set special?
A Items are ordered
B Items are unique no duplicates
2. How do you create an empty set?
A set()
B {}
3. {1, 2, 2, 3} becomes?
A {1, 2, 3}
B {1, 2, 2, 3}
4. Can you access set items by index?
A Yes like lists
B No sets are unordered
5. How do you add to a set?
A .add()
B .append()

Match the Pairs

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

Left columnRight column
1s = {1, 2, 3} print(s[0]) AError: {} creates dict, not set
2empty = {} BError: Sets have no index
3s = {1, 2, 2, 3} COutput: 3
4s = set() s.add(5) DOutput: True
5s = {3, 1, 2, 1} print(len(s)) EValid: Becomes {1, 2, 3}
6s = {1, 2, 3} print(2 in s) FOk: Empty set then add 5

Definitions

Set

Fill in the Blanks

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

1.
s = ___()
2.
s = {1, 2, 3} s.___(4)
3.
s = {1, 2, 3} s._______(2)
4.
print(3 __ {1, 2, 3})
5.
s = {1, 2, 2, 3} print(___(s))

Spot the Error

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

Question 1
A # Create an empty set
B s = {}
C s.add(1)
D print('Set:', s)
Question 2
A s = {1, 2, 3}
B s.append(4)
C print('Set:', s)
D print('Done')
Question 3
A s = {1, 2, 3}
B first = s[0]
C print('First:', first)
D print('Done')
Question 4
A s = {1, 2, 3}
B s.remove(5)
C print('Set:', s)
D print('Done')
Question 5
A s = {[1, 2], [3, 4]}
B label = 'Coords'
C print('Set:', s)
D print('Done')

Answer Key

Nested Lists
Section A: Quick Questions
Q1
B — Items are unique no duplicates
Q2
A — set()
Q3
A — {1, 2, 3}
Q4
B — No sets are unordered
Q5
A — .add()
Section B: Match the Pairs
Q1
s = {1, 2, 3} print(s[0]) → Error: Sets have no index
Q2
empty = {} → Error: {} creates dict, not set
Q3
s = {1, 2, 2, 3} → Valid: Becomes {1, 2, 3}
Q4
s = set() s.add(5) → Ok: Empty set then add 5
Q5
s = {3, 1, 2, 1} print(len(s)) → Output: 3
Q6
s = {1, 2, 3} print(2 in s) → Output: True
Section C: Definitions
Q1
Set — An unordered collection of unique items in curly braces.~~set()
Section D: Fill in the Blanks
Q1
s = set()
Q2
s = {1, 2, 3} s.add(4)
Q3
s = {1, 2, 3} s.discard(2)
Q4
print(3 in {1, 2, 3})
Q5
s = {1, 2, 2, 3} print(len(s))
Section E: Spot the Error
Q1
B — {} creates an empty dict, not a set. Use set() for an empty set.
Q2
B — Sets use .add(), not .append() (that's for lists)
Q3
B — Sets are unordered: you can't access items by index
Q4
B — 5 is not in the set: remove() raises KeyError. Use discard() instead.
Q5
A — Lists can't be in sets (unhashable). Use tuples instead.