In this article, we will see discuss how to return the length of the Set in Python. Set in Python is a one dimensional data structure that is stores unique values with multiple datatypes.
We can create a set using set()
function. or Simply by using {}
.
Syntax:
set_variable=set()
Here, we can add elements one by one using add()
method.
(or)
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.
# 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}
Using len() to find length of a set in Python
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.
# 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:
{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.
# 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:
{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.
# 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:
{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.
## 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:
{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.