Python - Set Methods Flashcards

1
Q

Return - none
Args - ( value to remove )
___________
searches for the given element in the set and removes it.

If the element(argument) passed to the remove() method doesn’t exist, keyError exception is thrown.

A

set.remove(element)

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

Return - none
Args - ( value to add )
___________
adds a given element to a set. If the element is already present, it doesn’t add any element.

A

set.add(element)

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

Return - none
Args - none
___________
makes a shallow copy. It doesn’t return any value.

A

set.copy()

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

Return - none
Args - none
___________
removes all elements from the set.

A

set.clear()

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

Return - set
Args - ( the set you wanna subtract out )
___________
returns the set difference of two sets.

A

A.difference(B)

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

Return - none
Args - ( value to remove from set )
___________
removes a specified element from the set (if present).

A

s.discard(x)

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

Return - set intersection
Args - ( sets to intersect with )
___________
returns the intersection of set A with all the sets (passed as argument).

A

A.intersection(*other_sets)

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

Return - bool
Args - ( second set or any iterable )
___________
returns True if two sets are disjoint sets. If not, it returns False.

You can also pass an iterable (list, tuple, dictionary and string) to disjoint(). The isdisjoint() method will automatically convert iterables to set and checks whether the sets are disjoint or not.

A

set_a.isdisjoint(set_b)

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

Return - bool
Args - ( superset you are taking subset from )
___________
returns True if all elements of a set are present in another set (passed as an argument). If not, it returns False.

A

A.issubset(B)

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

Return - bool
Args - ( subset )
___________
returns True if a set has every elements of another set (passed as an argument). If not, it returns False.

A

A.issuperset(B)

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

Return - random value that is popped
Args - none
___________
removes an random/arbitrary element from the set and returns the element removed.

A

set.pop()

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

Return - new set
Args - ( second set )
___________
returns a new set which is the symmetric difference of two sets.

The symmetric difference of two sets A and B is the set of elements which are in either of the sets A or B but not in both.

A

A.symmetric_difference(B)

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

Return - new set that is union of all sets
Args - ( other sets )
___________
returns a new set with distinct elements from all the sets.

A

A.union(*other_sets)

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

Return - none
Args - ( value to add to set, can be an iterable )
___________
adds elements from a set

A

A.update(B)

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

Return - bool
Args - ( iterable )
___________
returns True if any element of an iterable is True. If not, any() returns False.

A

any(iterable)

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

Return - bool
Args - ( iterable )
___________
returns True if all elements of an iterable is True. If not, any() returns False.

A

all(iterable)

17
Q

Return - enumerate object
Args - ( iterable )
___________
adds counter to an iterable and returns it (the enumerate object).

A

enumerate(iterable, start=0)

18
Q

Return - set
Args - ( iterable )
___________
constructs a Python set from the given iterable and returns it.

A

set( iterable )

19
Q

Return - sorted list
Args - ( iterable, optional key, optional reverse )
___________
returns a sorted list from the given iterable.

A

sorted( iterable, optional key, optional reverse)