Skip to content

Python set size | Example code

  • by

Use the len() method to Get the size of a Set in Python. Call len() built-in function and pass the set object as an argument. It returns the number of items in the set.

Python set size example code

Python program to find the length of a set.

my_set = {'A', 'B', 'C', 'D'}

print(len(my_set))

Output:

Python set size

How to find the length of a set in Python

Answer: You can find the length of the Python Set object using the len() function. Tuple as an element count only 1 value.

my_set = set()

# Adding element and tuple to the Set
my_set.add(1)
my_set.add(2)
my_set.add((4, 5))

print(my_set)
print(len(my_set))

Output:

{(4, 5), 1, 2}
3

Do comment if you have any doubts or suggestions on this Python set tutorial.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading