Storing multiple values of same or different types under a single name. They are collection of data.
Sets are unordered collection of unique data.
Enclosed by Curly braces {}
set: {10, 20.2, 30.0}
Each value in the set is called a element
Since set is unordered collection, it cannot be indexed. The elements are randomly stored.
Syntax:
set(iterable = None)
iterable: any data structures listed above
By default the {}
map to a dictionary. So empty curly braces cannot be used.
IN [2]
l = [10, 20, 30]
s = set(l)
s
IN [5]
s = {10, 20, 30, 10, 30}
s
s1 = set(input())
s2 = set(input().split())
There are various methods or functions that are used to work on sets.
1. add
2. update
3. discard
4. pop
5. clear
6. len
7. issubset
8. issuperset
9. union
10. intersection
11. intersection_update
12. difference
13. symmetric_difference
14. copy
15. isdisjoint
16. max
17. min
18. enumerate
19. sum
20. sorted
Adds an element to the end of the set.
Syntax: set.add(element)
IN [6]
set1 = {10, 20, 30}
set1.add(40)
set1
Adds elements in another set to given set
Syntax: set.update(set)
IN [8]
set1 = {10, 20, 30}
set2 = {10, 25, 40}
set1.update(set2)
set1
Discards the given element in the set.
Syntax: set.discard(element)
IN [9]
set1 = {10, 20, 30}
set1.discard(20)
set1
Removes and returns a random element from set.
Syntax: set.pop()
IN [12]
set1 = {10, 20, 30}
set1.pop()
Empties the set.
Syntax: set.clear()
IN [13]
set1 = {10, 20, 30}
set1.clear()
set1
Returns the length of the set
Syntax: len(set)
Checks if a set subset of another.
Syntax: set.issubset(set)
Or set1 <= set2
IN [14]
set1 = {10, 20, 30}
set2 = {10, 20}
set2.issubset(set1)
Checks if the set superset of another.
Syntax: set.issuperset(set)
Or set1 >= set2
IN [15]
set1 = {10, 20, 30}
set2 = {10, 20, 30, 35}
set2.issuperset(set1) # set1.issubset(set2)
Returns the union of 2 sets
Syntax: set.union(set)
Or set1|set2
IN [16]
set1 = {10, 20, 30}
set2 = {12, 22, 30}
set1.union(set2)
IN [18]
set1 = {10, 20, 30}
set2 = {12, 22, 30}
set1|set2
Returns the intersection of 2 sets
Syntax: set.intersection(set2)
Or set1 & set2
IN [17]
set1 = {10, 20, 30}
set2 = {12, 22, 30}
set1.intersection(set2)
IN [22]
set1 = {10, 20, 30}
set2 = {12, 22, 30}
set1 & set2
Updates the set with intersection of given set.
Syntax: set.intersection_update(set)
Or set1 &= set2
IN [24]
set1 = {10, 20, 30}
set2 = {12, 22, 30}
set1.intersection_update(set2)
set1
IN [25]
set1 = {10, 20, 30}
set2 = {12, 22, 30}
set1 &= set2
set1
Returns the set difference of given set
Syntax: set1-set2
Or set1 - set2
IN [26]
set1 = {10, 20, 30}
set2 = {12, 22, 30}
set1.difference(set2)
Returns the symmetric difference of two sets.
Syntax: set.symmetric_difference(set)
Or set1 ^ set2
IN [27]
set1 = {10, 20, 30}
set2 = {12, 22, 30}
set1.symmetric_difference(set2)
Copies a set
Syntax: set.copy()
IN [28]
set1 = {10, 20, 30}
set2 = set1.copy()
set2
Checks if the given sets are mutually exclusive.
Syntax: set.isdisjoint(set)
IN [29]
set1 = {10, 20, 30}
set2 = {1, 22, -37}
set1.isdisjoint(set2)
IN [1]
l = [10, -10, 20, -30, 40]
sum(l)
Returns the maximum value in the set.
Syntax: max(set, key = None)
IN [35]
l = {10, 15, 50, 21, -7}
max(l)
IN [36]
l = {10, 15, 50, 21, -7, 7}
max(l, key = lambda x: x % 5) # Maximum reminder when divided by 5
Returns minimum value in the iterable
Syntax: min(iterable, key = None)
IN [37]
l = {10, 15, 50, 21, -7}
min(l)
Returns the enumerate object for given set. An enumerate object is an iterable which contains ordered pair of the form (index, value)
Syntax: enumerate(iterable, start = 0)
IN [39]
l = {10, 20, 'Hello', 'a', -1}
list(enumerate(l))
returns the sum elements in the set.
Syntax: sum(set, start = 0)
It is a function used to sort.
Syntax: sorted(iterable, key = None, reverse = False)
Use for loop to access element.
Using * will convert the elements of into arguements of print method
IN [30]
set1 = {10, 20, 30}
for i in set1:
print(i)
stdout
10
20
30
IN [31]
set1 = {10, 20, 30}
print(*set1)
stdout
10 20 30
IN [32]
set1 = set(i+1 for i in range(0, 10))
set1
any checks if any of the element in an iterable is true.
all checks if all the element on an iterable is True
IN [33]
l = [10, 20, 30, 0]
print(any(l))
print(all(l))
stdout
True
False
IN [34]
set1 = {10, 20, 30, -1}
print(any(set1))
print(all(set1))
stdout
True
True