How to get the size of a set in Python


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! 👋

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.