A Set is a type of data container in Python which is used to store multiple data in one variable. It can contain elements of different data types. Elements in a set are unordered and hence it is not possible to access set's element using index number. Additionally, it does not allow multiple elements with same values (no duplicate elements).
Set can be created by separating it's elements by comma (,) and enclosing with curly bracket { }. Additionally, it can also be created using .
#Set with multiple datatypes Info = {'John', 25, 'London'} print(Info) #Creating set using constructor colors = set(('Red', 'Blue', 'Green')) print(colors)
{'John', 25, 'London'} {'Red', 'Blue', 'Green'}
An element of a Set can not be accessed using index number (indexing not allowed in a Set). However, elements of a Set can be accessed using for loop.
weekdays = {'MON', 'TUE', 'WED', 'THU', 'FRI'} for day in weekdays: print(day)
MON TUE WED THU FRI
Modifying element's value of a set is not allowed, however we can add or delete elements of a set.
Two methods can be used to add elements in a Set:
week = {'MON', 'TUE', 'WED'} week.add('SUN') # add this element in the set print(week) month = {'JAN', 'FEB', 'MAR', 'MAY'} # multiple elements are updated in the set (with no duplication). month.update(['JAN', 'NOV', 'DEC']) #Set is an unordered data container. print(month)
{'MON', 'TUE', 'WED', 'SUN'} {'JAN', 'FEB', 'MAR', 'MAY', 'NOV', 'DEC'}
Four methods and a keyword can be used to delete elements from a Set:
number = {10, 50, 100, 1000} number.remove(50) #delete 50 from the set. print(number) number = {10, 50, 100, 1000} number.discard(50) #delete 100 from the set. print(number) number = {10, 50, 100, 1000} number.pop() #delete last element from the set. print(number) number = {10, 50, 100, 1000} number.clear() #delete all elements from the set. print(number) number = {10, 50, 100, 1000} del number #delete set 'number' itself. print(number)
#Output after applying remove(50) on set 'number' {10, 100, 1000} #Output after applying discard(50) on set 'number' {10, 100, 1000} #Output after applying pop() on set 'number' {10, 50, 1000} #Output after applying clear()) on set 'number' set() #Output after applying del keyword on set 'number' NameError: name 'number' is not defined #please note that Set is an unordered data container.
The len() function can be used to find out total number of elements in a list, tuple, set or dictionary.
number = {10, 50, 100, 1000} print(len(number))
4
colors = {'Red', 'Blue', 'Green'} if 'white' in colors: print('Yes, white is an element of colors.') else: print('No, white is not an element of colors.')
No, white is not an element of colors.
There are several ways to join Sets.
colors = {'Red', 'Blue', 'Green'} numbers = {10, 20} mySet = colors.union(numbers) print(mySet) colors = {'Red', 'Blue', 'Green'} numbers = {10, 20} colors.update(numbers) print(colors)
{'Red', 'Blue', 'Green', 10, 20} #using union() method {'Red', 'Blue', 'Green', 10, 20} #using update() method
Methods | Description |
---|---|
A | Adds a specified element to the set |
Set | Deletes all the elements from the set |
is a type of data container in Python which is used to store multiple data in one variable. It can contain elements of different data types. Elements in a set are | Returns a copy of the set |
unordered | Returns a set containing all elements of the given set which are not present in the specified sets or iterables |
and hence it is not possible to access set's element using index number. Additionally, it does not allow multiple elements with same values ( | Deletes all elements from the given set which are present in the specified sets or iterables |
no duplicate elements | Remove the specified element from the given set |
). | Returns a set which contains common elements of two or more sets |
Deletes all elements from the given set which are not present in all specified set(s) | |
Create Set | Returns True when two sets have any common element, else returns False |
Returns True when all elements of the given set are present in specified set, else returns False | |
Set can be created by separating it's elements by comma (,) and enclosing with curly bracket { }. Additionally, it can also be created using | Returns True when all elements of the specified set are present in the given set, else returns False |
set() function | Deletes a random element from the given set |
. | Removes the specified element from the given set |
Returns a set with the symmetric differences of two sets | |
inserts the symmetric differences from the given set with another | |
Example | Return a set containing the union of sets |
Update the set with the union of this set and others | |
Functions | Description |
#Set with multiple datatypes Info = {'John', 25, 'London'} print(Info) #Creating set using constructor colors = set(('Red', 'Blue', 'Green')) print(colors) | Returns total number of elements in the set |
set function/constructor is used to create set from iterable like list, tuple, set, string, dictionary and range() etc. |