Python Sets
Python Sets
Python sets are containers of different or same type of elements. These elements are unordered which implies that they cannot be accesses using indexing as their position inside the set is not fixed. Sets do not allow duplicate elements in it.
Create Python Sets
Sets in Python are created by separating the elements with a comma and placing the elements inside a set of curly braces or by using the function set().
# Python profram to exemplify set creation #Set with different datatypes info = {'Katia', 21, 'New York'} print(info) #Creating set using constructor set() function info = set(('Katia', 21, 'New York')) print(info)
Output:
{'Katia', 21, 'New York'} {'Katia', 21, 'New York'}
Access Element of a Set
Elements of a set cannot be acquired using indexing since they are unordered. But they can be obtained in any order using a loop.
# Python program to exemplify access of set elements days = {'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'} for day in days: print(day)
Output:
Tuesday Thursday Sunday Monday Wednesday Friday Saturday
Modify Value of an Element
An element’s value cannot actually be modified in a set since you cannot access it as it us unordered, but addition and deletion of elements is required.
Add element to a Set
There are two ways to add an element to a set and they are:
-
-
- add() – takes one object as an argument and add it in the set
- update() – takes a list as an argument with a number of element and add them in the set
-
# Python program to exemplify adding elements in a set days = {'Monday', 'Tuesday', 'Wednesday'} days.add('Saturday') # add this element in the set print(days) month = {'January', 'February', 'March', 'April'} # multiple elements are updated in the set (with no duplication). month.update(['January', 'November', 'December']) #Set is an unordered data container. print(month)
Output:
{'Saturday', 'Monday', 'Tuesday', 'Wednesday'} {'February', 'March', 'December', 'November', 'January', 'April'}
Delete element from a Set
There are several ways to delete an element from a set and they are:
-
-
- remove() – deletes a particular element from a set specified as the argument inside it and returns an error if it is not present in the set
- discard() – deletes a particular element from a set specified as the argument inside it and returns an error if it is not present in the set
- pop() – deletes the last element from the set and since it is unordered, you never know which element is going to be deleted
- clear() – deletes all elements from a set
- del – deletes the set itself and displaying it using print() function will result in an error since the set will no longer be existing
-
numbers = {1, 2, 3, 4, 5} numbers.remove(5) #deletes 5 from the set. print(numbers) numbers = {1, 2, 3, 4, 5} numbers.discard(5) #deletes 5 from the set. print(numbers) numbers = {1, 2, 3, 4, 5} numbers.pop() #deletes last element from the set. print(numbers) numbers = {1, 2, 3, 4, 5} numbers.clear() #deletes all elements from the set. print(numbers) numbers = {1, 2, 3, 4, 5} del numbers #delete set 'numbers' itself. print(numbers)
Output:
{1, 2, 3, 4} {1, 2, 3, 4} {2, 3, 4, 5} set() Traceback (most recent call last): File "main.py", line 21, in <module> print(numbers) NameError: name 'numbers' is not defined
Python Sets Length
Length of a set in Python is obtained by using len() function.
# Python program to exemplify set length using len() function numbers = {1, 5, 10, 100} print(len(numbers))
Output:
4
Check an element in a Set
To check if an element is a part of a set or not is done using if statement along with in operator.
# Python program to exemplify if an element is a part of a set or not info = {'Evan', 28, 'Egypt'} if 'Evan' in info: print('Yes, Evan is an element of info.') else: print('No, Evan is not an element of info.')
Output:
Yes, Evan is an element of info.
Join Python Sets
Different ways to join two sets are as follows:
- union() – it is used to create a new set containing elements of both the sets set1 and set2
- update() – add elements of a set in the given set
# Python program to exemplify joining of two sets set_colors = {'Red', 'Blue', 'Green'} set_numbers = {1, 2} mySet = set_colors.union(set_numbers) print(mySet) set_colors = {'Red', 'Blue', 'Green'} set_numbers = {1, 2} set_colors.update(set_numbers) print(set_colors)
Output:
{1, 'Red', 2, 'Green', 'Blue'} {1, 'Red', 2, 'Green', 'Blue'}