The len() function in Python is used to get the length of the Set. Actually, length gets you the number of elements in the sequence/iterable which includes a python set. The len() function accepts only one parameter i.e iterable and return the total number of elements present in it. If the set is empty, the len() function returns 0.
Note that the len()
function is not the same as the length
attribute of an object that exists in other programming languages. In Python, the length
attribute does not exist, and you should always use the len()
function to get the number of elements in a Python Sets.
1. Quick Examples of Getting Length of Set
Following are quick examples of how to get the number of elements in a Set.
# Create an empty set
myset = {}
print("Total elements: ",len(myset))
# Create set with some elements
myset = {80,89,9,6.89}
print("Total elements: ",len(myset))
# Add one item
myset.add(92)
print("Total elements: ",len(myset))
# Add 5 elements to the set.
myset.update({110,210,310,670,500})
print("Total elements: ",len(myset))
# Find the total elements count which are greater than 300.
print("Count of values > 300:",len({i for i in myset if i > 300}))
2. Length of Python Set using len()
len()
function is a built-in function of Python that returns the total number of elements present in the set (length of set). It is important that we need to pass the set object as the parameter to this method. The len() function accepts only one parameter i.e. iterable and returns the total number of elements present in it. If the set is empty, the len() function returns 0.
2.1 Set len() Syntax
# Here, myset is the input set.
len(myset)
2.2 Set len() Parameters
It will take the set as an input parameter.
2.3 Set len() Return
Return an integer that represents the total number of elements.
3. Get Length of Python Set Example
By using the Python len() built-in function let’s see how to get the number of elements present in the empty Set and Set with some values.
3.1 Lenght of Empty Set
Let’s create an empty set and get the length of it. On empty Set the len() function return the length 0.
# Create an empty set
myset = {}
print("Set: ",myset)
# Return length of the set.
print("Total elements: ",len(myset))
# Output:
# Set: {}
# Total elements: 0
3.2 Set with Elements
Let’s have some elements in the set and apply the len() method to find the total elements present in the set.
# Create set with some elements
myset = {80,89,9,6.89,"welcome","to","sparkby","examples"}
print("Set: ",myset)
# Return length of the set.
print("Total elements: ",len(myset))
# Output:
# Set: {6.89, 9, 80, 'to', 'examples', 89, 'sparkby', 'welcome'}
# Total elements: 8
Let’s add elements using set.add() and set.update() methods and get the lenght. In each case return the total elements present in the set using len() method.
# Create set with some elements
myset = {80,89,9,6.89,"welcome","to","sparkby","examples"}
print("Set: ",myset)
# Return length of the set.
print("Total elements: ",len(myset))
# Add one element to the set.
myset.add("python")
print("Total elements: ",len(myset))
# Add 5 elements to the set.
myset.update({110,210,310,"four","five"})
print("Total elements: ",len(myset))
# Output:
# Set: {6.89, 9, 80, 'to', 'examples', 89, 'sparkby', 'welcome'}
# Total elements: 8
# Total elements: 9
# Total elements: 14
Actually there are 8 elements in the set.
- After adding one element using add() method to the set, the total number of elements in 9.
- After adding five elements using update() method to the set, the total number of elements in 14.
4. Get Length of Set using Comprehension
Let’s have some elements in the set and return the elements that are greater than 300 by specifying the condition inside the set comprehension.
# Create set with some values
myset = {80,89,9,6.89,234,567,890,32,45,78,300}
print("Total elements: ",len(myset))
# Find the total elements count which are greater than 300.
print("Total values greater than 300:",len({i for i in myset if i > 300}))
# Output:
# Total elements: 11
# Total values greater than 300: 2
Actually there are 11 elements in the set. The total number of elements that are greater than 300 are 2. The set comprehension that we specified inside the len() is {i for i in myset if i > 300}.
5. Conclusion
In this article, I have explained different ways to get the length of the Set in Python. We have seen different scenarios to return total number of elements present in the set using len() function. If you want to return only particular elements based on the condition, you can use Set Comprehension with len().
Related Articles
- Python Set add() – Add Element to Set
- Python Set update()
- Python Set pop() method
- Python Set remove() – Remove Element
- Python Set intersection() Explained
- Python Set Methods
- Check Two Sets are Equal in Python
- How to initialize Set in Python?
- How to subtract two sets in Python?
- Python set clear() method