Sets Flashcards

1
Q

Create set

A

set_variable = set()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Add value to set

A

set_variable.add(value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Check if value exists in set

A

value in set_variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Delete value from set

A

set_variable.remove(value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Remove duplicates from list

A

unique_list = list(set(original_list))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Find length of set

A

len(set_variable)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Clear set

A

set_variable.clear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Union set, returns new set which includes all elements from set1 and set2

A

union = set1.union(set2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Find the intersection of two sets

A

intersection = set1.intersection(set2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Find the elements that are in set1 but not set2

A

set1.difference(set2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Find the elements that aren’t shared by both sets

A

symmetric_difference = set1.symmetric_difference(set2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Find is set1 is a subset of superset of set2

A

set1.issubset(set2) # elements of set1 are all present in set2
set1.issuperset(set2) # elements of set2 are all present in set1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Create immutable set

A

frozen_set = frozenset([1, 2, 3])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Iterate over values in set

A

for value in set_variable:
print(value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly