set Data TypeThe set data type is an implementation of the mathematical concept of a set.
A variable of type set consists of a collection of elements delimited by curly braces { }, the elements being separated by commas.
The collection is unindexed and unordered.
The elements can be of different types.
Each element of the collection is enumerated once and only once.
An element must be a non-mutable object such as a numeric object (int, bool, float, complex), str or tuple.
Example
# create a set of integer type
primes = {2, 3, 5, 7}
print ('Prime numbers:')
print (primes)
# create a set of string type
vowels = {'a', 'e', 'i', 'o', 'u'}
print ('Vowels:')
print (vowels)
set() functionset() is a predefined function that takes a sequence object (list, tuple or str) as argument and returns a set object discarding duplicates.
Example
# tuple and list
primes = (2, 3, 5, 7)
vowels = ['a', 'e', 'i', 'o', 'u']
# create sets
primes_set = set(primes)
print(primes_set)
vowels_set = set(vowels)
print(vowels_set)
The add() method is used to add an element to a set.
Example
primes = {2, 3, 5, 7}
print('Initial Set:')
print(primes)
# using add() method
primes.add(11)
print('Updated Set:')
print(primes)
The update() method is used to add elements of a collection (list, tuple, str, set, etc.) to a set.
Example
primes = {2, 3, 5, 7}
print('Initial Set:')
print(primes)
more_primes = {11, 13, 17, 19}
# using update() method
primes.update(more_primes)
print('Updated Set:')
print(primes)
The discard() method is used to remove an element from a set.
Example
primes = {1, 2, 3, 5, 7}
print('Initial Set:')
print(primes)
# using discard() method
primes.discard(1)
print('Updated Set:')
print(primes)
The union of two sets A and B gives a set that contains all the elements that belong to either A or B.
The union() method returns as a result the union of two sets.
Example
first_primes = {2, 3, 5, 7}
more_primes = {11, 13, 17, 19}
# using union() method
primes = first_primes.union(more_primes)
print('Union Set:')
print(primes)
The intersection of two sets A and B gives a set that contains all elements that belong to both A and B.
The intersection() method returns as result the intersection of two sets.
Example
first_numbers = {0 ,1, 2, 3, 4, 5, 6, 7, 8, 9}
primes = {2, 3, 5, 7, 11, 13, 17, 19}
# using intersection() method
first_primes = primes.intersection(first_numbers)
print('Intersection Set:')
print(first_primes)
A set is limited and iterable: we can iterate through its elements in a for loop.
We use the membership operator in to iterate through the elements of a set.
Example
primes = {2, 3, 5, 7, 11, 13, 17, 19}
# iterate through set elements
for element in primes :
print(element, end=' ')
We can access set elements using list comprehension by converting the set into a list within the comprehension.
This allows us to iterate through the set elements and perform operations on them, similar to using a for loop.
Example
primes_set = {2, 3, 5, 7, 11, 13, 17, 19}
# access set elements using list comprehension
primes_list= [element for element in primes_set]
print(primes_list)
We can check whether a certain element is available in a set using the membership operators: in and not in.
Following are built-in methods for a set object.
Method
Description
set.add()
Add an element to a set.
set.clear()
Remove all elements from a set.
set.copy()
Return a shallow copy of a set.
set.discard()
Remove an element from a set if it is a member.
set.pop()
Remove and return an arbitrary set element.
set.remove()
Remove an element from a set; it must be a member.
The following are functions that take a set as argument.
Function
Description
all()
Returns True if all elements in the set are True or if the set is empty.
any()
Returns True if any element in the set is True. If the set is empty, returns False.
enumerate()
Returns an enumerate object that contains the index and value of all elements in the set in pairs.
len()
Returns the number of elements in the set.
max()
Returns the greatest element in the set.
min()
Returns the smallest element in the set.
sorted()
Returns a list object sorted by the elements of the set (does not sort the set itself).
sum()
Returns the sum of all elements in the set.
Example
s = {2, 3, 5, 7}
e = enumerate(s)
print(type(e))
# print the tuples in enumerate object
for elem in e:
print (elem)