How to determine the length of a set in Python
In this tutorial, we are going to learn about how to get the length of a set in Python with the help of examples.
In Python, a set is a collection of unchangeable, unindexed, unordered data. The sets are defined using the currly braces {}
.
Consider, we have a following set in our Python code:
usersSet = {"apple", "grapes", "kiwi"}
Using the len() function
To get the length of a set, we can use the len() function in Python.
The len() function takes the set as an argument and returns the number of items in it.
Here is an example:
usersSet = {"apple", "grapes", "kiwi"}
length = len(usersSet)
print(length)
Output:
3