Membership operators (in, not in) in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use membership operators like in or not in, we check if a value exists inside a collection.
We want to know how the time it takes changes as the collection gets bigger.
Analyze the time complexity of the following code snippet.
def check_membership(item, collection):
if item in collection:
return True
else:
return False
items = [1, 2, 3, 4, 5]
print(check_membership(3, items))
This code checks if item is inside collection and returns True or False.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each element in the collection one by one.
- How many times: Up to all elements in the collection until the item is found or the end is reached.
As the collection gets bigger, the number of checks grows roughly the same as the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The work grows directly with the size of the collection.
Time Complexity: O(n)
This means the time to check membership grows in a straight line as the collection gets bigger.
[X] Wrong: "Membership checks always happen instantly no matter the collection size."
[OK] Correct: For lists or tuples without special structure, Python checks items one by one, so bigger collections take more time.
Understanding how membership checks scale helps you write efficient code and explain your choices clearly in interviews.
"What if we changed the collection from a list to a set? How would the time complexity change?"
Practice
in operator do in Python?Solution
Step 1: Understand the purpose of
Theininoperator is used to check if a value is present inside a collection like a list, string, or tuple.Step 2: Compare with other options
Options A, B, and C describe actions unrelated to membership checking.Final Answer:
Checks if a value exists inside a collection -> Option DQuick Check:
inmeans membership check [OK]
in means 'is inside' [OK]- Confusing
inwith adding or removing items - Thinking
inchanges the collection - Mixing
inwith comparison operators
'apple' is NOT in the list fruits?Solution
Step 1: Recall correct syntax for
The correct syntax to check absence isnot invalue not in collection.Step 2: Evaluate each option
if 'apple' not in fruits: matches correct syntax. if 'apple' not fruits: missesin. if not 'apple' fruits: missesin. if 'apple' !in fruits: uses invalid operator!in.Final Answer:
if 'apple' not in fruits: -> Option AQuick Check:
Usenot inas two words [OK]
not in as two words, no symbols [OK]- Writing
!ininstead ofnot in - Omitting
inafternot - Using
not 'value' inwhich is valid but less clear
letters = ['a', 'b', 'c']
print('d' in letters)
print('a' not in letters)Solution
Step 1: Check if 'd' is in the list
'd' is not in ['a', 'b', 'c'], so'd' in lettersis False.Step 2: Check if 'a' is not in the list
'a' is in the list, so'a' not in lettersis False, soprint('a' not in letters)prints False.Final Answer:
False False is incorrect because the second print outputs False, but the first print outputs False, so the correct output isFalse Falsewhich matches False\nFalse.Quick Check:
'd' in letters= False,'a' not in letters= False [OK]
- Assuming 'd' is in the list
- Mixing up
inandnot inresults - Confusing True/False outputs
items = ['pen', 'pencil', 'eraser']
if 'pen' not items:
print('Pen is missing')Solution
Step 1: Check the membership syntax
The correct syntax for checking absence isvalue not in collection. The code missesinafternot.Step 2: Verify other parts
List name and print statement are correct. The only error is missingin.Final Answer:
Missinginafternot-> Option CQuick Check:
Usenot intogether [OK]
not in together [OK]- Writing
not itemsinstead ofnot in items - Forgetting
inkeyword - Assuming
notalone checks membership
words = ['cat', 'dog', 'bird']. You want to print all words that are NOT in the string text = 'I have a dog and a cat'. Which code correctly does this?Solution
Step 1: Understand the goal
We want to print words from the list that are NOT found inside the stringtext.Step 2: Check each option's logic
for w in words: if w not in text: print(w) usesif w not in text, which correctly checks absence. for w in words: if w in text: print(w) prints words that ARE in text, opposite of goal. for w in words: if not w text: print(w) has syntax error, missinginafterw. for w in words: if w not text: print(w) has syntax error missingin.Final Answer:
for w in words: if w not in text: print(w) -> Option AQuick Check:
Usenot infor absence check [OK]
if w not in text to find missing words [OK]- Omitting
inkeyword in various positions - Forgetting
inkeyword - Printing words that are present instead of absent
