codingstreets
Search
Close this search box.
python-set

An Introduction to Python Set with Practical Examples

In this article you will learn about Python Set

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)
python-set
As it is shown clearly that it returned different order of elements every time as set is not ordered. 

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)
python-set
As it is shown clearly that it returned different order of elements every time as set is not ordered. 

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

z = {'a', 'b', 'c'}
print('a' in z)
python-set
As it is shown clearly that it returned True as element ‘a’ is present in set. 

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.

Example 1- Add element in set using add() method.

z = {'a', 'b', 'c'}
z.add('d')

print(z)
python-set
As it is shown clearly that it returned updated version of set.

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

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

print(z)
python-set
As it is shown clearly that it returned updated version of set.

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))
python-set
As it is shown clearly that it returned length of the set.

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)
python-set
As it is shown clearly that it removed element from set.

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'}
z.discard('c')

print(z)
python-set
As it is shown clearly that it removed element ‘c’ and returned a new set.

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)
python-set
As it is shown clearly that ordered is not same therefore, different element is deleted.

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)
python-set
As it is shown clearly that it cleared the whole set.

Example 5- Use del keyword to delete set.

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

print(z)
python-set
As it is shown clearly that set is deleted therefore, it returned NameError.

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)
python-set
As it is shown clearly that it returned a new set containing all elements from both sets. But it is changing elements as set is not ordered.

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)
python-set
As it is shown clearly that it updated old string with itself; a new string defined with same name.  

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

As it is shown clearly that it returned a new set containing all elements from both sets. But it is changing elements as set is not ordered.

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)
python-set
As it is shown clearly it returned a set, constructed with set construct.

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

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on