Sets in Python3
Sets in Python
Sets in Python 3 are a built-in data type that represent an unordered collection of unique elements. They are defined using curly braces {} or the set() constructor.
Here are some of the main features and uses of sets in Python:
-
Uniqueness: Sets enforce that all elements they contain are unique, meaning that duplicates are automatically removed.
-
Membership testing: You can use the
inoperator to check if an element is in a set, and thenot inoperator to check if it is not. -
Set operations: You can perform common set operations such as union (
|), intersection (&), difference (-), and symmetric difference (^) on sets. -
Modification: You can add elements to a set using the
add()method, and remove elements using theremove()ordiscard()methods. -
Iteration: You can iterate over the elements of a set using a for loop, just like with lists and other iterable objects.
Here's a simple example to demonstrate the basic features of sets in Python (In Python Console):
>>> s = {1, 2, 3}
>>> print(s)
{1, 2, 3}
>>> 2 in s
True
>>> 4 in s
False
>>> s.add(4)
>>> print(s)
{1, 2, 3, 4}
>>> s.remove(4)
>>> print(s)
{1, 2, 3}
>>> for item in s:
... print(item)
...
1
2
3
Sets are useful for tasks such as removing duplicates from a list, testing for membership, and performing set operations.
Create A Set
we can create a set using {} brackets or using set() constructor
a = {10,"python",23.22,40,True} # create a set
print(a) # {True, 40, 10, 'python', 23.22}
a = set([10,"python",23.22,40,True]) # set constructor
print(a) # {True, 40, 10, 'python', 23.22}
All Set Built In Methods
| Method | Uses |
|---|---|
| add() | Add an element in the set |
| clear() | Removes all the elements from the set |
| copy() | Create a copy of the set |
| difference() | find difference between two or more sets and return new set |
| difference_update() | Removes the common elements in specified set and add in existing |
| discard() | Remove the specified item |
| intersection() | Return intersection of two other sets |
| intersection_update() | Removes all elements except common with specified set(s) |
| isdisjoint() | Returns True if two sets have a intersection else False |
| issubset() | Returns True if another set contains this set else False |
| issuperset() | Returns True if set contains another set else False |
| pop() | Removes random element from the set and return |
| remove() | Removes the specified element from the set or raise error |
| symmetric_difference() | Returns a set with the symmetric differences of two sets except common items |
| symmetric_difference_update() | remove elements from a set if present or insert with specified set |
| union() | Return a set containing the union of sets |
| update() | Update the set with the union of this sets |
Sets methods Examples in Python
Set Add() Method
add() method add new element in set
a = {True, 40, 10, 'python', 23.22}
a.add(88)
print(a) # {True, 40, 10, 23.22, 88, 'python'}
Set Pop() Method
pop() method Remove a random element from a set and return
a = { 40, 10, 'python', 23.22}
r = a.pop()
print(r) # 40
Set Clear() Method
remove all element from sets .
a = {True, 40, 10, 'python', 23.22}
a.clear()
print(a) # set()
Set Difference() Method
return the difference of two sets as new set . remove common elements.
a = {True, 40, 10, 'python', 23.22}
b = {10,20,435,"python"}
print(a.difference(b)) # {40, True, 23.22}
Set Difference_update() Method
remove duplicate item from two sets with specified set. it does not return new set.
a = {True, 40, 10, 'python', 23.22}
b = {10,20,435,"python"}
a.difference_update(b)
print(a) # {True, 40, 23.22}
Set Intersection() Method
intersection() method return the intersection of two sets. intersection_update() method modify existing set it does not return new .
a = {True, 40, 10, 'python', 23.22}
b = {10,20,435,"python"}
print(a.intersection(b)) # {'python', 10}
Set Isdisjoint() Method
Return True if a sets is disjoint with specified set.
a = {True, 40, 10, 'python', 23.22}
b = {10,20,435,"python"}
print(a.isdisjoint(b)) # False
Set Union() Method
return the union of a set with specified set(All items). duplicate value excluded.
a = {True, 40, 10, 'python', 23.22}
b = {10,20,435,"python"}
print(a.union(b)) # {True, 40, 10, 'python', 435, 20, 23.22}

