# PYTHON - Sets

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).

## Create Set

Set can be created by separating it's elements by comma (,) and enclosing with curly bracket { }. Additionally, it can also be created using set() function.

#Set with multiple datatypes
Info = {'John', 25, 'London'}
print(Info)

#Creating set using constructor
colors = set(('Red', 'Blue', 'Green'))
print(colors)


Output

{'John', 25, 'London'}

{'Red', 'Blue', 'Green'}


## Access element of a Set

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)


Output

MON
TUE
WED
THU
FRI


## Modify element's value

Modifying element's value of a set is not allowed, however we can add or delete elements of a set.

### Add element of a set:

Two methods can be used to add elements in a Set:

• update() - add element(s) to the Set. Please note that, Set does not support duplicate elements.

week = {'MON', 'TUE', 'WED'}
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)


Output

{'MON', 'TUE', 'WED', 'SUN'}

{'JAN', 'FEB', 'MAR', 'MAY', 'NOV', 'DEC'}


## Delete element of a set

Four methods and a keyword can be used to delete elements from a Set:

• remove() - deletes specified element from the Set. Returns an error if the element is not present in the set.
• discard() - deletes specified element from the Set. Returns no error if the element is not present in the set.
• pop() - deletes last element from the Set. Please note that, Set is an unordered data container therefore last element is not predefined.
• clear() - deletes all elements of the set.
• del - deletes the set itself.

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

#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.


## Set Length

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))


Output

4


## Check an element in the Set

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.')


Output

No, white is not an element of colors.


## Join Sets

There are several ways to join Sets.

• union(): union() method is used to create new set containing set1 and set2.
• update(): update() method is used to add elements of a set in a given set.

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)


Output

{'Red', 'Blue', 'Green', 10, 20}  #using union() method
{'Red', 'Blue', 'Green', 10, 20}  #using update() method


### Recommended Pages

Set Methods & Functions
MethodsDescription
clear() Deletes all the elements from the set
copy() Returns a copy of the set
difference() Returns a set containing all elements of the given set which are not present in the specified sets or iterables
difference_update() Deletes all elements from the given set which are present in the specified sets or iterables
discard() Remove the specified element from the given set
intersection() Returns a set which contains common elements of two or more sets
intersection_update() Deletes all elements from the given set which are not present in all specified set(s)
isdisjoint() Returns True when two sets have any common element, else returns False
issubset() Returns True when all elements of the given set are present in specified set, else returns False
issuperset() Returns True when all elements of the specified set are present in the given set, else returns False
pop() Deletes a random element from the given set
remove() Removes the specified element from the given set
symmetric_difference() Returns a set with the symmetric differences of two sets
symmetric_difference_update() inserts the symmetric differences from the given set with another
union() Return a set containing the union of sets
update() Update the set with the union of this set and others
FunctionsDescription
len() Returns total number of elements in the set
set() set function/constructor is used to create set from iterable like list, tuple, set, string, dictionary and range() etc.