Open In App

Time Complexity for Adding Element in Python Set vs List

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

Time complexity is a crucial factor in algorithm analysis, helping us understand how the performance of an algorithm scales with input size. In Python, lists and sets are two commonly used data structures for storing collections of elements. Let’s explore and compare the time complexity of adding elements to lists and sets in various scenarios.

Time Complexity For Adding Elements To List Vs Set In Python

Below, are the examples of Time Complexity For Adding Elements To a List vs. a set In Python.

Lists in Python

Example 1: Appending Elements to the End of a List

Appending an element to the end of a list has an amortized time complexity of O(1). Python lists are implemented as dynamic arrays, and adding an element to the end usually takes constant time, but occasionally the array may need to be resized, resulting in an O(n) operation.

Python3




my_list = []
my_list.append(1)
 
print(my_list)


Output

[1]




Example 2: Inserting Elements at a Specific Position

Inserting an element at a specific position in a list takes O(n) time. This is because all elements following the insertion point need to be shifted to make room for the new element.

Python3




my_list = [1, 2, 3]
my_list.insert(1, 4)
 
print(my_list)


Output

[1, 4, 2, 3]




Example 3: Extending a List

Extending a list with another iterable takes O(k) time, where k is the length of the iterable being added.

Python3




my_list = [1, 2, 3]
my_list.extend([4, 5])
 
print(my_list)


Output

[1, 2, 3, 4, 5]




Sets in Python

Example 1 : Adding Elements to a Set

Adding an element to a set generally has an average-case time complexity of O(1). However, in rare cases where hash collisions occur, it might be O(n), but such cases are infrequent.

Python3




my_set = {1, 2, 3}
my_set.add(4)
 
print(my_set)


Output

{1, 2, 3, 4}




Example 2:Updating a Set with Another Set

Updating a set with another set takes O(len(set2)) time. The time complexity is proportional to the size of the second set.

Python3




set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1.update(set2)
 
print(set1)


Output

{1, 2, 3, 4, 5}




Example 3: Union of Sets

The union operation creates a new set containing all distinct elements from both sets. It has a time complexity of O(len(set1) + len(set2)), as it essentially involves creating a new set and adding all elements from both sets.

Python3




set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
 
print(union_set)


Output

{1, 2, 3, 4, 5}




Conclusion

In conclusion, both lists and sets in Python offer different time complexities for adding elements. Lists are efficient for appending elements at the end, while sets shine in scenarios where unique elements are crucial. Understanding these complexities helps in choosing the right data structure based on specific use cases and performance requirements.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads