Python – Find Length of a Set

To find the length of a Set in Python, you can call Python built-in function len() and pass the set as argument to the function. len() function returns an integer representing the number of elements in the Set.

Example

In the following program, we initialize a Python Set aSet with some integers and find the length of this Set using len() function.

Program

# initialize set
aSet = {'apple', 'cherry', 'banana'}

# find set length
n = len(aSet)

# print the length
print(f'Length of the set is : {n}')
Try Online

Output

Length of the set is : 3
ADVERTISEMENT

References

Conclusion

In this Python Tutorial, we learned how to find the length of a given Set using len() function with the help of example Python programs.