Search

An Introduction to Python Set with Practical Examples

Python Set – Before moving ahead, let’s take a look at another Python collection – Python Tuple

Set – It is collection of unordered and unindexed elements. It is defined with the curly braces {}.

Example – Creating a set.

```x = {'a', 'b', 'c'}
print(x)```

Note:  Since set is unordered. It will change element order every time.

Access elements – Like list and tuple elements cannot be accessed by defining index number of specified value because set is unordered.

But through ‘for’ loop and ‘in’ keyword elements can be accessed.

Example 1- Get the elements of set through ‘for’ loop.

```z = {'a', 'b', 'c'}
for x in z:
print(x)```

Example 2- Get the elements of set through ‘for’ loop.

```z = {'a', 'b', 'c'}
print('a' in z)```

Change elements – Once set is created its elements cannot be changed.

```Add elements - It is used to add one element in set.

add() method - It is used to add one element in set.

update() method - It is used to add more than one element in set.```

```z = {'a', 'b', 'c'}

print(z)```

Example 2- Add elements in set using update() method.

```z = {'a' , 'b' , 'c'}
z.update(['d', 'e'])

print(z)```

Length of set – To get the length of the set use len() method.

Example – Get the length of the set by using len() method.

```z = {'a', 'b', 'c'}
print(len(z))```

remove() method – To remove an element from set.

```Syntax – set.remove(‘str’)

Parameter Value –

Str – It takes the element that has to be removed.```

Example – Use of remove() method to remove an element from set.

```z = {'a' , 'b' , 'c'}
z.remove('b')

print(z)```

Note: If the element to remove does not exist, remove() method raises an error.

discard() method– To remove the specified element from the set.

```Syntax – set.discard(‘str’)

Parameter Value –

Str – It takes the element that has to be removed.```

Example – Use of discard method to remove an element from set.

```z = {'a' , 'b' , 'c'}

print(z)```

Note: If the element to remove does not exist, discard() method raises an error.

pop() method – To remove last element from set use pop() method but it cannot delete any fix element because set is unordered.

```Syntax – set.pop()

Parameter Value – No value```

Example – Use of pop() method to remove an element from set.

```z = {'a' , 'b' , 'c'}
z.pop()

print(z)```

Note: Set is unordered. Any element can be removed from set.

clear() method – It used to empty whole set.

```Syntax – set.clear()

Parameter Value – No value```

Example – Use of clear() method to empty set.

```z = {'a' , 'b' , 'c'}
x = z.clear()

print(x)```

Example 5- Use del keyword to delete set.

```z = {'a', 'b', 'c'}
del z

print(z)```

Join Two Sets – In Python there are two methods to join sets.

union() method – It returns new set containing all elements from both sets ( existing set and new set).

```Syntax – str.union(str)

Parameter Value –

Str – It takes the set as an argument.```

Example – Use union() method to join two sets.

```old_set = {'a' , 'b' , 'c'}
new_set = {1 , 2 , 3}
updated_set = old_set.union(new_set)

print("Updated set is:", updated_set)```

update() method – It updates existing set with itself (a new set with same name) and also take new set to update elements of existing set by transferring new set element to existing set.

```Syntax – str.update(str)

Parameter Value –

Str – It takes the set as an argument.```

Example 1- Use update() method to update a set with itself (a new set with same name).

```old_set = {'a' , 'b' , 'c'}
old_set = {1 , 2 , 3}
old_set.update(old_set)

print("Updated set is:", old_set)```

Example 2- Use update() method to join two sets.

```old_set = {'a' , 'b' , 'c'}
new_set = {1 , 2 , 3}
old_set.update(new_set)

print("Updated set is:", old_set)```

Set() Constructor – It is use to make a set using constructor.

Example – Make a set using set() constructor –

```z = set(('a' , 'b' , 'c' , 'd'))
print(z)```

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Share on