Python Set Methods Flashcards
Adds an element to the set
add()
Removes all the elements from the set
clear()
Returns a copy of the set
copy()
Returns a set containing the difference between two or more sets
difference()
Removes the items in this set that are also included in another, specified set
difference_update()
Remove the specified item
discard()
Returns a set, that is the intersection of two or more sets
intersection()
Removes the items in this set that are not present in other, specified set(s)
intersection_update()
Returns whether two sets have a intersection or not
isdisjoint()
Returns whether another set contains this set or not
issubset()
Returns whether this set contains another set or not
issuperset()
Removes an element from the set
pop()
Removes the specified element
remove()
Returns a set with the symmetric differences of two sets
symmetric_difference()
inserts the symmetric differences from this set and another
symmetric_difference_update()
Return a set containing the union of sets
union()
Update the set with another set, or any other iterable
update()
What is a python set?
- Sets are used to store multiple distinct items in a single variable.
- A set is a collection which is unordered, unchangeable*, and unindexed.
- Set items are unchangeable, but you can remove items and add new items.
- Sets are written with curly brackets.
- Set items are unordered, unchangeable, and do not allow duplicate values.
- Since sets are unordered, so you cannot be sure in which order the items will appear.
- Set items can appear in a different order every time you use them, and cannot be referred to by index or key.
- Sets cannot have two items with the same value.
- Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage.
- To determine how many items a set has, use the len() function.
- Set items can be of any data type:
How are sets created?
- Sets are written with curly brackets
myset = {“apple”, “banana”, “cherry”} - It is also possible to use the set() constructor to make a set.
thisset = set((“apple”, “banana”, “cherry”)) # note the double round-brackets