Python – Set

Python – Set

Set in Python is an unordered and non-indexed collection of elements that are non-duplicate. It is a Mutable data-type.

Creating a Set:

a) Using a { }: Set can be created using curly brackets, serparated by comma.

# Defining a set
example_set = {'a', 'b', 'c',
               'd', 'e'}

# Print the set
print(example_set)

Output:

{'d', 'b', 'c', 'e', 'a'}

b) Using a set() function: Set can be created using set() built-in function.

# Defining a set
example_set = set()

# Print data-type
print(type(example_set))

# Print set
print(example_set)

Output:

<class 'set'>
set()

How to access items in a Set?

Elements in a set cannot be accessed directly because there is no concept of indexing as the elements are stored in unordered fashion in sets. So, we can use for/while loop to iterate in a set.

# Defining a set
example_set = {'a', 'b', 'c', 
               'd', 'e'}

# Iterate in the set 
# and print the elements
for i in example_set :
    print(i)

Output:

d
c
b
a
e

Adding a new item in a Set:

a) Using add() Method: If you want to add a single item only then use add() method of set.

# Defining a set
example_set = {'a', 'b', 'c',
              'd', 'e'}

# Adding one item 'f'
# in a given set
example_set.add('f')

# Print the result
print(example_set)

Output:

{'f', 'e', 'a', 'b', 'c', 'd'}

b) Using update() Method: If you want to add more than one items at once then use update() method of set.

# Defining a set
example_set = {'a', 'b', 'c',
               'd', 'e'}

# Adding multiple items 
example_set.update(['f', 'g', 'h'])

# Print the result
print(example_set)

Output:

{'f', 'e', 'a', 'b', 'c', 'd'}

Remove an item from the set:

a) Using remove() method: This method is used to remove given element from the set. If the given element, which we want to be removed, doesn’t exist then this method will raise an error.

# Defining a set
example_set = {'a','b','c','d','e'}

# Remove a given element 
# from the set
example_set.remove("b")

# Print the result
print(example_set)

Output:

{'e', 'a', 'd', 'c'}

b) Using discard() method: This method is used to remove given element from the set. If the given element, which we want to be removed, doesn’t exist then this method will not raise any error in such a case.

# Defining a set
example_set = {'a', 'b', 'c', 
              'd', 'e'}

# Remove given item from the set
example_set.discard('c')

# Print the result
print(example_set)

Output:

{'e', 'a', 'd', 'b'}

c) Using pop() method: This method will remove the last item from the set. But, we never know which element is being removed since a set is unordered. This method will return the popped/removed value.

# Defining a set
example_set = {'a', 'b', 'c', 
               'd', 'e'}

# Pop a random element
#  from the set
x = example_set.pop()

# Print the value removed
print(x)

Output:

b

Join two sets:

a) Using union() method: To create a new set containing all the unique elements of both sets.

# Create two sets
s1 = {'a','b','c'}
s2 = {1, 2, 3}

# Join Two sets
s3 = s1.union(s2)

# Print the resultant set
print(s3)

Output:

{1, 'a', 2, 'c', 'b', 3}

b) Using | (pipe) symbol: When this symbol is used with sets then this symbol perform union operation on sets. So, It create a new set containing all the unique elements of both sets.

# Create two sets
s1 = {'a','b','c'}
s2 = {1, 2, 3}

# Join Two sets using |(pipe)
# symbol
s3 = s1 | s2

# Print the resultant set
print(s3)

Output:

{'b', 1, 2, 3, 'a', 'c'}

c) Using update() method: This method is used to insert all the items of a set into another set.

# Create two sets
s1 = {'a','b','c'}
s2 = {1, 2, 3}

# Join the sets
s1.update(s2)

# Print the resultant set
print(s1)

Output:

{'c', 1, 2, 'b', 3, 'a'}

Find the length of a set:

For finding length of set in Python we are using len() built-in function.

# Defining a set
example_set = {'a','b','c','d','e'}

# Print the length of the set
print(len(example_set))

Output:

5

Set Deletion:

For deleting the entire set we are using del keyword. If we try to print the deleted set, it will show the NameError that the set is not defined.

# Defining a set
example_set = {'a','b','c',
              'd','e'}

print(example_set)

# Delete the set
del example_set

# Print set
print(example_set

Output:

{'e', 'c', 'a', 'd', 'b'}
Traceback (most recent call last):
  File "main.py", line 11, in 
    print(example_set)
NameError: name 'example_set' is not defined

Leave a Reply

Your email address will not be published.