How to find length of Set in Python?

Use len() function to check length of a set in Python

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

How to find length of Set in Python?

In this article, we will see discuss how to return the length of the Set in Python. A Python set is a one-dimensional data structure that stores unique values with multiple datatypes. The built-in len() function works on sets the same way it does on lists and strings.

We can create a set using set() function. or Simply by using {}.

Syntax:

bash
set_variable=set()

Here, we can add elements one by one using add() method.
(or)

bash
set_variable={}

Here, we can add elements inside it separated by a comma.

If we try to add the duplicate elements into the set, they will be removed internally. So we can't expect any error in this case.

python
# Create a set
my_set=set()
# add 5 elements to the set
my_set.add(10)
my_set.add(20)
my_set.add(30)
my_set.add(40)
my_set.add(50)

# Create a set with 5 elements
my_set={10,20,30,40,50}
Output

Using len() to find length of a set in Python

<a href="https://docs.python.org/3/library/functions.html#len" target="_blank" rel="noopener"

len() method in Set is used to return the total number of elements present in a Set.

Syntax:

len(set_data)

Parameter:

Here, len() takes set data structure as only one parameter.

Return:

Return an integer that represents the total number of elements in a set.

Example-1

Python Program to create a Set with 10 elements and return the total number of elements in a set.

python
# Create a set
my_set={"golinux-cloud",2,3,4,5,"deepak","sravan","prasad",45,67}

# Display the set
print(my_set)

# Return length of the set
print(len(my_set))
Output

Output:

{2, 3, 4, 5, 'golinux-cloud', 67, 45, 'prasad', 'deepak', 'sravan'}
10

Explanation:

We created a set that stores integer and string type elements.

First print statement displays the elements in a set and second print statement results the total number of elements that exists in the set. As per the output, there are 10 elements in the set.

Example 2

Python Program to create a Set with 10 elements that include duplicates and return the total number of elements in a set.

python
# Create a set
my_set={"golinux-cloud",2,3,4,5,"golinux-cloud",2,3,4,5,}

# Display the set
print(my_set)

# Return length of the set
print(len(my_set))
Output

Output:

{2, 3, 4, 5, 'golinux-cloud'}
5

Explanation:

We created a set that stores integer and string type elements. There are 5 duplicates in it. As we know that set will not allow duplicates.

First print statement displays the elements in a set and second print statement results the total number of elements that exists in the set. As per the output, there are 5 unique elements in the set.

Example 3

Python Program to create an empty set and add 5 elements to it. Display the total number of elements in the set.

python
# Create a set
my_set=set()

# add 5 elements to the set
my_set.add(10)
my_set.add(20)
my_set.add(30)
my_set.add(40)
my_set.add(50)

# Display the set
print(my_set)

# Return length of the set
print(len(my_set))
Output

Output:

{40, 10, 50, 20, 30}
5

Explanation:

We created a set that stores 5 integer type elements.

First print statement displays the elements in a set and second print statement results the total number of elements that exists in the set. As per the output, there are 5 elements in the set.

Example 4

Python Program to create an empty set and add 5 elements that include duplicates to it. Display the total number of elements in the set.

python
## Create a set
my_set=set()

# add 5 elements to the set
my_set.add(10)
my_set.add(20)
my_set.add(20)
my_set.add(50)
my_set.add(50)

# Display the set
print(my_set)

# Return length of the set
print(len(my_set))
Output

Output:

{10, 20, 50}
3

Explanation:

First print statement displays the elements in a set and second print statement results the total number of elements that exists in the set. As per the output, there are only 3 elements in the set among 5 added elements.


Summary

In this guide, we seen how to apply len() function on a set to return total number of elements with four examples. If there are duplicate elements in set, set will remove them and return the total number of unique elements.

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …