Python - Set Methods Flashcards Preview

Python II > Python - Set Methods > Flashcards

Flashcards in Python - Set Methods Deck (19)
Loading 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)

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)

3
Q

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

A

set.copy()

4
Q

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

A

set.clear()

5
Q

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

A

A.difference(B)

6
Q

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

A

s.discard(x)

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)

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)

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)

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)

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()

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)

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)

14
Q

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

A

A.update(B)

15
Q

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

A

any(iterable)

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)