Find the length of a set in Python



Find the length of a set in Python

In Python, a Set is a collection data type that is unordered and mutable. A set cannot have duplicate elements. Here, the task is to find out the number of elements present in a set. See the below examples.

Examples:

 

Input: a = {1, 2, 3, 4, 5, 6}
Output: 6

Input: a = {'Geeks', 'For'}
Output: 2

Thee idea is use len() in Python

 

 

Find-Length-of-set

Example 1:

# Python program to find the length
# of set
 
set1 = set()  
   
# Adding element and tuple to the Set 
set1.add(8
set1.add(9
set1.add((6, 7)) 
 
print("The length of set is:", len(set1))

Output:

The length of set is: 3

Example 2:

n = len({1, 2, 3, 4, 5})
 
print("The length of set is:", n)

Output:

The length of set is: 5

How does len() work?
len() works in O(1) time as the set is an object and has a member to store its size. Below is description of len() from Python docs.

Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

Last Updated on March 1, 2022 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs