Python Set count (length)

Python len gives the number of items in a set.

s = {'a', 'b', 'c'}

x = len(s)

print(x)  # 3

len is used to count the elements of a list, dictionary, set, and so on.

Duplicates are not counted

If a set contains the duplicates, these are regarded as one item.

s = {'a', 'b', 'c', 'a'}

x = len(s)

print(x)  # 3

Length of empty set

t = set()

y = len(t)

print(y)  # 0

The length of empty set is 0. Empty set can be defined as set().


Powered by Markdown