Could someone explain the time complexity of the following loop?
for x in iterable:
if x not in other_iterable:
return False
I found a really good Python operation time complexity text lecture here, and saw that the time for the outer for
loop was O(N). However, how does the if x not in other_iterable
part factor into the time complexity? I imagine the loop will be checking x
against every element in iterable
until it is found, or the list is exhausted. So what would be the recommended way to make the if x not in other_iterable
loop take the smallest amount of time possible? Possibly having sorted the other_iterable
? I'm practically a rookie at understanding time complexity, and would like to know more.
Edit: other_iterable
would be a list with possible duplicates.