0

A simple sample program. I want to know if the "in" keyword makes the program of O(n) complexity when used here.

dummyList = [1, 4, 8, 7, 2]

num = 5

if num in dummyList:

    print("found")
  • It depends. Is the length of `dummyList` constant, or is it allowed to change? If your input length can't change, big-O notation doesn't even apply at all in the first place -- because its purpose is to describe how runtime changes in accordance with changes in input. But clearly, the worst-case time for `if num in dummyList` gets slower the longer `dummyList` is when `dummyList` is in fact a list. – Charles Duffy Sep 12 '22 at 21:18

0 Answers0