What is the complexity of the in operator in Python? Is it theta(n)?
Is it the same as the following?
def find(L, x):
for e in L:
if e == x:
return True
return False
L is a list.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The complexity of
independs entirely on whatLis.e in Lwill becomeL.__contains__(e).See this time complexity document for the complexity of several built-in types.
Here is the summary for
in:The O(n) worst case for sets and dicts is very uncommon, but it can happen if
__hash__is implemented poorly. This only happens if everything in your set has the same hash value.