Python Tutorial Python Advanced Python References Python Libraries

Python - Set Length



The Python len() function can be used to find out the total number of elements in the set. To add more to this, len() function returns the number of elements in an iterable. An iterable object can be any data structure like list, tuple, set, string and dictionary, etc.

Syntax

len(set)

Parameters

set Required. specify iterable like a set to find out total number of elements in it.

Return Value

Returns the total number of elements in the set.

Example:

In the example below, len() function is used on the set called MySet to find out the total number of elements in it.

MySet = {10, 20, 30, 40, 50, 60}

#estimates total number of elements in the set.
print(len(MySet))

The output of the above code will be:

6

❮ Python Set Methods