Python Set Methods Flashcards

1
Q

add()

A

The add() method adds an element to the set.

set.add(elmnt)

fruits = {“apple”, “banana”, “cherry”}

fruits.add(“orange”)

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

clear()

A

The clear() method removes all elements in a set.

set.clear()

fruits = {“apple”, “banana”, “cherry”}

fruits.clear()

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

copy()

A

The copy() method copies the set.

set.copy()

fruits = {“apple”, “banana”, “cherry”}

x = fruits.copy()

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

difference()

A

The difference() method returns a set that contains the difference between two sets.

set.difference(set)

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.difference(y)

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

difference_update()

A

The difference_update() method removes the items that exist in both sets.

set.difference_update(set)

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.difference_update(y)

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

discard()

A

The discard() method removes the specified item from the set.

set.discard(value)

fruits = {“apple”, “banana”, “cherry”}

fruits.discard(“banana”)

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

intersection()

A

The intersection() method returns a set that contains the similarity between two or more sets.

set.intersection(set1, set2 … etc)

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.intersection(y)

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

intersection_update()

A

The intersection_update() method removes the items that is not present in both sets (or in all sets if the comparison is done between more than two sets).

set.intersection_update(set1, set2 … etc)

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.intersection_update(y)

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

isdisjoint()

A

The isdisjoint() method returns True if none of the items are present in both sets, otherwise it returns False.

set.isdisjoint(set)

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "facebook"}

z = x.isdisjoint(y)

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

issubset()

A

The issubset() method returns True if all items in the set exists in the specified set, otherwise it retuns False.

set.issubset(set)

x = {"a", "b", "c"}
y = {"f", "e", "d", "c", "b", "a"}

z = x.issubset(y)

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

issuperset()

A

The issuperset() method returns True if all items in the specified set exists in the original set, otherwise it retuns False.

set.issuperset(set)

x = {"f", "e", "d", "c", "b", "a"}
y = {"a", "b", "c"}

z = x.issuperset(y)

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

pop()

A

The pop() method removes a random item from the set.

set.pop()

fruits = {“apple”, “banana”, “cherry”}

fruits.pop()

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

remove()

A

The remove() method removes the specified element from the set.

set.remove(item)

fruits = {“apple”, “banana”, “cherry”}

fruits.remove(“banana”)

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

symmetric_difference()

A

The symmetric_difference() method returns a set that contains all items from both set, but not the items that are present in both sets.

set.symmetric_difference(set)

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.symmetric_difference(y)

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

symmetric_difference_update()

A

The symmetric_difference_update() method updates the original set by removing items that are present in both sets, and inserting the other items.

set.symmetric_difference_update(set)

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.symmetric_difference_update(y)

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

union()

A

The union() method returns a set that contains all items from the original set, and all items from the specified set(s).

set.union(set1, set2…)

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.union(y)

17
Q

update()

A

The update() method updates the current set, by adding items from another set (or any other iterable).

set.update(set)

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.update(y)