Chapter 11 - Dictionaries and Sets

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/58

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 4:26 PM on 7/19/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

59 Terms

1
New cards

dictionaries

built-in data structures used to store data in key-value pairs

  • each key maps to a value, and the values can be accessed and modified using the key

2
New cards

How do you create dictionaries?

by enclosing key-value pairs in curly braces or by using the built-in dict() constructor

3
New cards

creating dictionary using curly braces

user_profile = {
     "username": "coder123",
     "followers": 2500,
     "is_active": True
}

4
New cards

creating dictionary using dict()

car_info = dict(brand="Ford", model="Mustang", year=1964)

5
New cards

using dictionary with user input

word = input("Please type an English word: ")
if word in translation-dict:
     print("Translation: ", translation_dict[word])
else: 
     print("Word not found")

6
New cards

using strings as keys and integers as values

scores = {}
scores["Alice"] = 85
scores["Bob"] = 92
scores["Charlie"] = 78

7
New cards

using integers as keys and lists as values

collections = {}
collections[1] = ["apple", "banana"]
collections[2] = ["pear", "peach", "plum"]
collections[3] = ["grape", "orange"]

8
New cards

True or false: a key can appear multiple times in a dictionary

false

9
New cards

What happens if a new entry uses an existing key?

the original value is replaced

10
New cards

What is the output for this code?

translation_dict["big"] = "grande"
translation_dict["big"] = "enorme"
print(translation_dict["big"])

enorme

11
New cards

True or false: all keys in a dictionary must be immutable

true

12
New cards

What data type cannot be used in dictionaries?

lists

13
New cards

What is the output for this code?

translation_dict[[1,2,3]] = 5

TypeError: unhashabletype: ‘list’

14
New cards

What is the output for this code?

translation_dict = {"cat": "gato", "dog": "perro", "bird": "pájaro}
for key in translation_dict:
     print("key:", key)
     print("value:", translation_dict[key])

key: cat

value: gato

key: dog

value: perro

key: bird

value: pájaro

15
New cards

How can you remove key_value pairs?

using del

16
New cards

What is the output for this code?

staff = {"Alan": "lecturer", "Emily": "professor", "David": "lecturer"}
del staff["David"]
print(staff)

{‘Alan’: ‘lecturer’, ‘Emily’: ‘professor’}

17
New cards

checking if a key exists before deletion

if "Paul" in staff:
     del staff["Paul"]
     print("Deleted")
else:
     print("This person is not a staff member")

18
New cards

What is the output for this code?

staff = {"Alan": "lecturer", "Emily": "professor", "David": "lecturer"}
removed = staff.pop("David", None)
if removed is None:
     print("This person is not a staff member")
else:
     print(removed, "removed")

lecturer removed

19
New cards

What is the output for this code?

person1 = {"name": "Alice Smith", "height": 165, "weight": 68, "age": 29}
person2 = {"name": "Bob Johnson", "height": 180, "weight": 85, "age": 34}
person3 = {"name": "Catherine Lee", "height": 158, "weight": 54, "age": 22}
people = [person1, person2, person3]
for person in people:
     print(person["name"])
combined_height = 0
for person in people:
     combined_height += person["height"]
print("The average height is", combined_height/len(people))

Alice Smith

Bob Johnson

Catherine Lee

The average height is 167.6666666666666

20
New cards

key() method

returns a view object of all keys

21
New cards

values() method

returns a view object of all key-value pairs

22
New cards

items() method

returns a view object of all key-value pairs

23
New cards

What is the output for this code?

my_dict = {"name": "Alice", "age": 25, "city": "New York"}
print(my_dict.keys())
print(my_dict.values())
print(my_dict.items())

dict_keys([‘name’, ‘age’, ‘city’])

dict_values([‘Alice’, 25, ‘New York’])

dict_items([(‘name’, ‘Alice’),(‘age’, 25), (‘city’, ‘New York’)])

24
New cards

How do you check if a key is in a dictionary?

use the in keyword

25
New cards

How do you check if a value is in a dictionary?

use the in keyword with the values() method

26
New cards

What is the output for this code?

my_dict = {"name": "Alice", "age": 25, "city": "New York"}
print("name" in my_dict)
print(25 in my_dict

True

True

27
New cards

get() method

used to retrieve a value for a given key

syntax: dict.get(key, default_value)

28
New cards

What is the output for this code?

my_dict = {"name": "Alice", "age": 25, "city": "New York"}
print(my_dict.get("name")
print(my_dict.get("country", "USA")

Alice

USA

29
New cards

setdefault() method

retrieves a value for a given key, and if the key does not exist, inserts the key with a specified value

syntax: dict.setdefault(key, default_value)

30
New cards

What is the output for this code?

my_dict = {"name": "Alice", "age": 25}
print(my_dict.setdefault("city", "New York"))
print(my_dict)

New York

{‘name’: ‘Alice’, ‘age’: 25, ‘city’: ‘New York’}

31
New cards

nested dictionary

dictionary within another dictionary

32
New cards

What is the output for this code?

family = {
     "child1": {"name": "Alice", "age": 5},
     "child2": {"name": "Bob", "age": 8}
print(family["child1"]["child2']

Alice

33
New cards

How can dictionaries be used?

to count occurrences of each item (use.get()) and to categorize items by their intial letter

34
New cards

sets

built-in data structures that store unordered collections of unique items

35
New cards

True or false: sets can have duplicate items

false

36
New cards

True or false: the elements contained in a set must be of an immutable test

true

37
New cards

How can a set be created?

either by using the built-in set function or by using curly braces {}

38
New cards

What is the output for this code?

x = set(('dog','cat','bird','dog'))
print(x)

{‘dog’, ‘cat’, ‘bird’}

39
New cards

Why must an empty set be defined using the set() function?

because Python interprets empty curly braces as an empty dictionary

40
New cards

True or false: lists and dictionaries can be elements of a set

false; set elements must be mutable

41
New cards

What does the len() function return for a set?

its number of elements

42
New cards

What is the output for this code?

x = {'apple', 'banana', 'cherry'}
print(len(x))

3

43
New cards

What is the output for this code?

x = {'apple', 'banana', 'cherry'}
print('banana' in x)
print('orange' in x)

True

False

44
New cards

a|b or a.union(b)

returns a new set containing all unique items from two or more sets

45
New cards

What is the output for this code?

a = {'red', 'blue', 'green'}
b = {'green', 'yellow', 'purple'}
print(a|b)

{‘red’, ‘green’, ‘purple’, ‘blue’, ‘yellow’}

46
New cards

a & b or a.intersection() method

returns a set that contains all elements common to both a and b

47
New cards

What is the output for this code?

a = {1,2,3,4}
b = {2,4,6,8}
print(a & b)

{2, 4}

48
New cards

a-b or a.difference(b)

returns a set that contains items that only exist a and not in b

49
New cards

What is the output for this code?

a = {1,2,3,4}
b = {2,4,6,8}
print(a-b)

{1,3}

50
New cards

a^b or .symmetric_difference() method

returns a set that contains all elements in either a or b, but not both

51
New cards

What is the output for this code?

a = {1,2,3,4}
b = {2,4,6,8}
print(a^b)

{1, 3, 6, 8}

52
New cards

.isdisjoint() method

returns true if sets a and b don’t have any elements in common and their intersection is an empty set

53
New cards

What is the output for this code?

a = {1,2,3,4}
b = {2,4,6,8}
c = {1,2,3}
d = {7,8,9}

print(c.isdisjoint(d))
print(a.isdisjoint(b))

True

False

54
New cards

dictionary comprehension

concise, single-line syntax used to create new dictionaries from an iterable

syntax: {key: value for variable in iterable}

55
New cards

What is the output for this code?

squares = {x: x*x for x in range(6)}
print(squares)

{0:0, 1:1, 2:4, 3:9, 4:16, 5:25}

56
New cards

What is the output for this code?

values = ['apple', 'banana', 'cherry']
dictionary = {i: value for i, value in enumerate(values)}
print(dictionary)

{0: ‘apple’, 1:’banana’, 2:’cherry’}

57
New cards

set comprehension

concise, single-line syntax used to create a new set from an existing iterable

  • written like list comprehensions but with curly braces instead of parentheses

58
New cards

What is the output for this code?

set1 = set([1,2,3,4,5])
set2 = {item for item in set1}
print(set2)

{1,2,3,4,5}

59
New cards

What is the output for this code?

set1 = set([1,20,2,40,3,50])
set2 = {item for item in set1 if item < 10}
print(set2)

{1, 2, 3}