Python Set – len() Function

It is a function in python that returns the length of the given set. It returns a integer type value, that is the number of elements in the set and it doesn’t modifies the given set.

Syntax:
len(set)

Also Read: Python Set – clear() Method

Example 1:

# Python code to understand len() method in set
# www.codewindow.in

s = {"amazing", "code"}
s1 = {"window", "hustle", "repeat"}
x = len(s)
x1 = len(s1)
print(x)
print(x1)

Output:

2
3

Explanation:
Here the first set has 2 elements in it and the second set has 3 elements in it. Hence it returned 2 then 3.

Example 2:

# Python code to understand len() method in set
# www.codewindow.in

s = {1, 8, 4, 1, 45, 85}
x = len(s)
print(x)

Output:

5

Explanation:
Here there are 5 unique elements in the set. Since a set doesn’t have any repeated elements, the number “1” which is repeated twice has been counted only once.


Follow Us

You Missed

Also Checkout