This content originally appeared on DEV Community and was authored by DEV Community
Python sets methods
add()
- Adds an element to the set.
smileys = {"π", "π", "π", "π€©"}
smileys.add("π")
# output
{'π', 'π', 'π', 'π€©', 'π'}
clear()
- Removes all the elements from the set.
smileys = {"π", "π", "π", "π€©"}
smileys.clear()
# output
set()
copy()
- Returns a copy of the set.
smileys = {"π", "π", "π", "π€©"}
x = smileys.copy()
# output
{'π', 'π', 'π', 'π€©'}
difference()
- Returns a set containing the difference between two or more sets.
smileys = {"π", "π", "π", "π€©"}
emojis = {"π", "π", "π", "π", "π€©", "π"}
x = emojis.difference(smileys)
# output
{'π', 'π'}
difference_update()
- Removes the items in this set that are also included in another, specified set.
- The difference_update() method is different from the difference() method, because the difference() method returns a new set.
- without the unwanted items, and the difference_update() method removes the unwanted items from the original set.
emojis = {"π", "π", "π", "π", "π€©", "π"}
smileys = {"π", "π", "π€©"}
emojis.difference_update(smileys)
# output
{'π', 'π', 'π'}
discard()
- Remove the specified item.
smileys = {"π", "π", "π", "π€©"}
smileys.discard("π")
# output
{'π', 'π', 'π€©'}
intersection()
- Returns a set, that is the intersection of two or more sets.
smileys = {"π", "π", "π", "π€©"}
emojis = {"π", "π", "π"}
x = smileys.intersection(emojis)
# output
{'π'}
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).
- The intersection_update() method is different from the intersection() method, because the intersection() method returns a new set.
- without the unwanted items, and the intersection_update() method removes the unwanted items from the original set.
smileys = {"π", "π", "π", "π€©", "π"}
emojis = {"π", "π", "π"}
smileys.difference_update(emojis)
# output
{'π€©', 'π', 'π'}
isdisjoint()
- Returns whether two sets have a intersection or not
smileys = {"π", "π", "π"}
emojis = {"π", "π", "π€©"}
x = smileys.isdisjoint(emojis)
# output
True
issubset()
- method returns True if all items in the set exists in the specified set, otherwise it retuns False.
smileys = {"π", "π", "π"}
emojis = {"π", "π", "π€©"}
x = smileys.issubset(emojis)
# output
False
pop()
- Removes an element from the set.
- Note: Because the set() is unordered we cannot ensure what element will be removed.
smileys = {"π", "π", "π", "π€©", "π"}
x = smileys.pop()
# output 1
π
# output 2
π€©
# output 3
π
remove()
- Removes the specified element.
smileys = {"π", "π", "π", "π€©", "π"}
smileys.remove("π")
# output
{'π', 'π', 'π', 'π€©'}
symmetric_difference()
- method returns a set that contains all items from both set, but not the items that are present in both sets. -Meaning: The returned set contains a mix of items that are not present in both sets.
smileys = {"π", "π", "π", "π€©", "π"}
emojis = {"π", "π", "π€©"}
x = smileys.symmetric_difference(emojis)
# output
{'π', 'π', 'π', 'π'}
symmetric_difference_update()
- method updates the original set by removing items that are present in both sets, and inserting the other items.
smileys = {"π", "π", "π", "π€©", "π"}
emojis = {"π", "π", "π€©", "π"}
smileys.symmetric_difference_update(emojis)
# output
{'π', 'π', 'π'}
union()
- method returns a set that contains all items from the original set, and all items from the specified set(s).
- Note: It will remove the same element from the original set.
smileys = {"π", "π", "π", "π"}
emojis = {"π", "π", "π€©", "π"}
x = smileys.union(emojis)
# output
{'π', 'π', 'π', 'π', 'π', 'π€©'}
update()
- method updates the current set, by adding items from another set (or any other iterable).
smileys = {"π", "π"}
emojis = {"π", "π", "π€©", "π"}
smileys.update(emojis)
# output
{'π', 'π', 'π€©', 'π', 'π', 'π'}
All the best to you.
This content originally appeared on DEV Community and was authored by DEV Community
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.

APA
MLA
DEV Community | Sciencx (2022-02-22T23:27:02+00:00) Python sets methods explanation and visualization. Retrieved from https://www.scien.cx/2022/02/22/python-sets-methods-explanation-and-visualization/
" » Python sets methods explanation and visualization." DEV Community | Sciencx - Tuesday February 22, 2022, https://www.scien.cx/2022/02/22/python-sets-methods-explanation-and-visualization/
HARVARDDEV Community | Sciencx Tuesday February 22, 2022 » Python sets methods explanation and visualization., viewed ,<https://www.scien.cx/2022/02/22/python-sets-methods-explanation-and-visualization/>
VANCOUVERDEV Community | Sciencx - » Python sets methods explanation and visualization. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/22/python-sets-methods-explanation-and-visualization/
CHICAGO" » Python sets methods explanation and visualization." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/02/22/python-sets-methods-explanation-and-visualization/
IEEE" » Python sets methods explanation and visualization." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/02/22/python-sets-methods-explanation-and-visualization/. [Accessed: ]
rf:citation » Python sets methods explanation and visualization | DEV Community | Sciencx | https://www.scien.cx/2022/02/22/python-sets-methods-explanation-and-visualization/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.