Python Set Methods Flashcards
add()
The add() method adds an element to the set.
set.add(elmnt)
fruits = {“apple”, “banana”, “cherry”}
fruits.add(“orange”)
clear()
The clear() method removes all elements in a set.
set.clear()
fruits = {“apple”, “banana”, “cherry”}
fruits.clear()
copy()
The copy() method copies the set.
set.copy()
fruits = {“apple”, “banana”, “cherry”}
x = fruits.copy()
difference()
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)
difference_update()
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)
discard()
The discard() method removes the specified item from the set.
set.discard(value)
fruits = {“apple”, “banana”, “cherry”}
fruits.discard(“banana”)
intersection()
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)
intersection_update()
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)
isdisjoint()
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)
issubset()
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)
issuperset()
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)
pop()
The pop() method removes a random item from the set.
set.pop()
fruits = {“apple”, “banana”, “cherry”}
fruits.pop()
remove()
The remove() method removes the specified element from the set.
set.remove(item)
fruits = {“apple”, “banana”, “cherry”}
fruits.remove(“banana”)
symmetric_difference()
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)
symmetric_difference_update()
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)