Time Complexity of In Operator in Python
Last Updated :
02 Dec, 2024
The in operator in Python is commonly used to check for membership within collections such as lists, sets, dictionaries and strings. Understanding the time complexity of the in operator is crucial for writing efficient code, especially when dealing with large datasets. In this article, we'll explore about different data structures and time complexity of using in operator with them.
Complexity of in operator in lists
Time Complexity: O(n)
Lists in Python are implemented as dynamic arrays. When you use the in operator to check if an element exists in a list, Python performs a linear search which means it iterates through each element of the list until it finds a match or reaches the end of the list.
Example:
Python
li= [1, 2, 3, 4, 5]
print(3 in li) # True
print(6 in li) # False
The time complexity is O(n) because in the worst case, the element might be at the end of the list or not present at all requiring a full traversal of all n elements.
Complexity of in operator in Sets
Time Complexity: O(1) on average
Sets in Python are implemented using hash tables. The in operator for sets leverages the hash function to determine if an element exists, providing average case constant time complexity.
Example:
Python
s= {1, 2, 3, 4, 5}
print(3 in s) # True
print(6 in s) # False
The average-case time complexity is O(1) because hash table lookups are generally fast. However, in the rare case of many hash collisions, the time complexity can degrade to O(n).
Complexity of in operator in Dictionaries
Time Complexity: O(1) on average
Similar to sets, dictionaries in Python are also implemented using hash tables. The in operator checks if a key exists in the dictionary, providing average case constant time complexity.
Example:
Python
dict = {'a': 1, 'b': 2, 'c': 3}
print('b' in dict) # True
print('d' in dict) # False
The average case time complexity is O(1) for key lookups due to the efficient hashing mechanism. However, in cases of excessive hash collisions, it could degrade to O(n).
Complexity of in operator in Strings
Time Complexity: O(m * n)
Strings in Python are arrays of characters. When using the in operator to check if a substring exists within a string, Python performs a substring search, which can be quite complex.
Example:
Python
s= "hello world"
print("world" in s) # True
print("python" in s) # False
The time complexity is O(m * n), where n is the length of the string and m is the length of the substring being searched for.
Similar Reads
Measure time taken by program to execute in Python Measuring the execution time of a Python program is useful for performance analysis, benchmarking, and optimization. Python provides several built-in modules to achieve this with ease. In this article, we'll explore different ways to measure how long a Python program takes to run.Using the time Modu
2 min read
Checking Element Existence in a Python Set We are given a set and our task is to check if a specific element exists in the given set. For example, if we have s = {21, 24, 67, -31, 50, 11} and we check for the element 21, which is present in the set then the output should be True. Let's explore different ways to perform this check efficiently
2 min read
Date difference in minutes in Python To calculate the date difference in minutes in Python, subtract two datetime objects to get the time difference. Then, convert that difference into minutes or break it down into minutes and seconds based on your needs. For example: for two dates, 2025-05-03 18:45:00 and 2025-05-03 16:30:00, the time
3 min read
Python Access Set Items Python sets are unordered collections of unique elements. Unlike lists or dictionaries, sets do not have indices so accessing elements in the traditional way using an index doesn't apply. However, there are still ways to work with sets and access their items effectively. This article will guide you
2 min read
Python - Time Strings to Seconds in Tuple List Given Minutes Strings, convert to total seconds in tuple list. Input : test_list = [("5:12", "9:45"), ("12:34", ), ("10:40", )] Output : [(312, 585), (754, ), (640, )] Explanation : 5 * 60 + 12 = 312 for 5:12. Input : test_list = [("5:12", "9:45")] Output : [(312, 585)] Explanation : 5 * 60 + 12 = 3
7 min read
Python time.perf_counter_ns() Function Python time.perf_counter_ns() function gives the integer value of time in nanoseconds. Syntax: from time import perf_counter_ns We can find the elapsed time by using start and stop functions. We can also find the elapsed time during the whole program by subtracting stoptime and starttime Example 1:
1 min read