COMP 110 QUIZ 03

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/19

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

20 Terms

1
New cards
Ordered data structure allowing duplicates
list
2
New cards
Check if a key exists in a dictionary
if "key" in my_dict:
3
New cards

Which is faster for checking if an item exists: list or set

set

4
New cards

Code to count word frequencies using a dict

counts = {}
for word in words: 
	counts[word] = counts.get(word, 0) + 1

5
New cards
Add an element to a set
my_set.add("item")
6
New cards

Loop through a dict's keys and values

for key, value in my_dict.items(): 
	print(key, value)

7
New cards
Output of len(set([1,2,3,2]))
3 (sets remove duplicates)
8
New cards
Best data structure for a shopping cart
list (ordered, allows duplicates)
9
New cards
Best data structure for username→userID mapping
dict (key-value pairs)
10
New cards
Best data structure for unique visitor IPs
set (uniqueness)
11
New cards

How to remove 'apple' from this set? fruits = {'apple', 'banana'}

fruits.remove("apple")

12
New cards
Error with print(my_dict["c"]) if "c" doesn't exist
KeyError (use my_dict.get("c"))
13
New cards

Create an empty set

my_set = set() NOT {}

14
New cards
Add a key-value pair to a dict
my_dict["new_key"] = value
15
New cards
Check if a value exists in a list
if value in my_list:
16
New cards
Remove duplicates from a list
list(set(my_list)) (order not preserved)
17
New cards
Output of {"a":1, "b":2} == {"b":2, "a":1}
True (dicts compare key-value pairs, not order)
18
New cards

How to get all keys from a dictionary

my_dict.keys()

19
New cards

Merge two sets

set1.union(set2)

20
New cards

Delete a key from a dict

mydict.pop("key")