Python Tutorial Python Advanced Python References Python Libraries

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)

The output of the above code will be:

{'London', 'John', 25}
{'Blue', 'Red', '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)

The output of the above code will be:

MON
THU
TUE
FRI
WED 

Modify value of an Element

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:

  • add() - add an element to the Set.
  • update() - add element(s) to the Set. Please note that, Set does not support duplicate elements.
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)

The output of the above code will be:

{'SUN', 'WED', 'MON', 'TUE'}
{'NOV', 'MAY', 'JAN', 'MAR', 'DEC', 'FEB'}

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)

The above code will give the following output:

{1000, 10, 100}
{1000, 10, 100}
{10, 100, 50}
set()

Traceback (most recent call last):
  File "Main.py", line 19, in <module>
    print(number)
NameError: name 'number' is not defined

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

The output of the above code will be:

4

Check an element in the Set

If control statement is used to check whether the set contains specified element or not.

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

The above code will give the following 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)

The output of the above code will be:

{'Green', 10, 20, 'Red', 'Blue'}
{'Green', 10, 20, 'Red', 'Blue'}



More Concepts on Python Sets