Open In App

Complexity Of Len() With Regard To Sets And Lists

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The len() function in Python is a built-in function used to determine the number of elements in a given data structure. When it comes to sets and lists, the complexity of len() can vary. For lists, the len() function operates in constant time, providing a quick and efficient way to retrieve the number of elements regardless of the list’s size. On the other hand, for sets, the complexity of len() is also constant on average but may involve hash collisions in rare cases, leading to slightly higher time complexity.

In this article, we will explore the complexity Of Len() About Sets And Lists.

Complexity of len() in Lists

Lists in Python are implemented as dynamic arrays. The length of the list is stored as a separate attribute within the list object. This attribute is updated whenever elements are added to or removed from the list. When you use the len() function, it simply retrieves the pre-stored length attribute, resulting in constant-time complexity.

Python




class myClass:
    def __init__(self):
        self.elements = []
        self.length = 0
 
    def add_element(self, element):
        self.elements.append(element)
        self.length += 1
 
    def remove_element(self, element):
        self.elements.remove(element)
        self.length -= 1
 
    def custom_len(self):
        return self.length
 
# Example usage:
my_list = myClass()
 
# Adding elements to the list
my_list.add_element(1)
my_list.add_element(2)
my_list.add_element(3)
 
# Length of the list
print("my_list Length: ", my_list.custom_len())
 
# Removing an element from the list
my_list.remove_element(2)
 
# Length of the modified list
print("my_list Length: ", my_list.custom_len())


Output:

my_list Length:  3
my_list Length: 2

Time Complexity: O(1)

The time complexity of the len() operation on lists is constant because it doesn’t depend on the size of the list. The length attribute is maintained and updated in constant time as elements are added or removed.

Space Complexity: O(1)

The space complexity of the len() operation on lists is also constant. It doesn’t require additional space proportional to the size of the list. The length attribute is a fixed-size attribute associated with the list object.

Complexity of len() in Sets

Sets in Python are implemented as hash tables. Like lists, sets also have a separate attribute to store the size or length of the set. When elements are added or removed, the size attribute is adjusted accordingly. The len() function for sets retrieves this pre-stored size, resulting in constant-time complexity.

Python3




class myClass:
    def __init__(self):
        self.elements = set()
        self.length = 0
 
    def add_element(self, element):
        self.elements.add(element)
        self.length += 1
 
    def remove_element(self, element):
        self.elements.remove(element)
        self.length -= 1
 
    def custom_len(self):
        return self.length
 
# Example usage:
my_set = myClass()
 
# Adding elements to the list
my_set.add_element(1)
my_set.add_element(2)
my_set.add_element(3)
 
# Length of the list
print("my_set Length: ", my_set.custom_len())
 
# Removing an element from the list
my_set.remove_element(2)
 
# Length of the modified list
print("my_set Length: ", my_set.custom_len())


Output:

my_set Length:  3
my_set Length: 2

Time Complexity: O(1)

The time complexity of the len() operation on sets is constant because it doesn’t depend on the size of the set. The size attribute is maintained and updated in constant time as elements are added or removed.

Space Complexity: O(1)

The space complexity of the len() operation on sets is also constant. It doesn’t require additional space proportional to the size of the set. The size attribute is a fixed-size attribute associated with the set object.

Conclusion

In conclusion, the time complexity of len() is considered constant in both Sets and Lists because the operation does not depend on the size of the data structure. It provides a quick way to retrieve the number of elements without iterating through the entire collection. However, time complexity can vary for other operations on lists and sets. For example, iterating over the elements of a list or set would have a linear time complexity, O(n), where n is the number of elements in the collection. However, the len() function itself is optimized for quick access to the size of these data structures.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads