To get the size of a set
in Python, you can call the len()
function
and pass the set
as its argument.
set_size = len(set)
print(set_size) # The size of the set
Let’s see a quick example. Suppose you have a Python set defined as follows:
my_set = {1, 2, 5, 6}
To get the size of my_set
, call the len()
function and assign the result in a variable:
set_size = len(my_set)
print(set_size) # 4
As you can see, the len()
function correctly returned the size of my_set
, which is 4
.
I hope this tutorial helps. See you in other tutorials! 👋