We use list length to know how many items are in a list. Membership test helps us check if an item is inside the list.
List length and membership test in Python
Start learning this pattern below
Jump into concepts and practice - no test required
my_list = [item1, item2, item3] length = len(my_list) if element in my_list: # do something
len() is a built-in function that returns the number of items in the list.
The in keyword checks if an element exists in the list and returns True or False.
my_list = [] print(len(my_list)) # Output: 0 print(5 in my_list) # Output: False
my_list = [10] print(len(my_list)) # Output: 1 print(10 in my_list) # Output: True
my_list = [1, 2, 3, 4, 5] print(len(my_list)) # Output: 5 print(1 in my_list) # Output: True print(5 in my_list) # Output: True print(6 in my_list) # Output: False
This program creates a shopping list, prints its length, and checks if certain items are in the list.
def main(): shopping_list = ['milk', 'eggs', 'bread'] print(f"Shopping list has {len(shopping_list)} items.") item_to_check = 'eggs' if item_to_check in shopping_list: print(f"{item_to_check} is in the shopping list.") else: print(f"{item_to_check} is NOT in the shopping list.") item_to_check = 'butter' if item_to_check in shopping_list: print(f"{item_to_check} is in the shopping list.") else: print(f"{item_to_check} is NOT in the shopping list.") if __name__ == '__main__': main()
Time complexity for len() is O(1) because Python stores the list size.
Membership test in takes O(n) time because it checks each item until it finds a match or reaches the end.
Common mistake: Using == instead of in to check membership.
Use len() when you need the count of items. Use membership test to check presence before actions like removal or update.
len() tells how many items are in a list.
in checks if an item is inside the list and returns True or False.
These tools help you work with lists safely and correctly.
Practice
What does the len() function do when used with a list in Python?
Solution
Step 1: Understand the purpose of
Thelen()len()function counts how many elements are inside a list.Step 2: Compare with other options
Checking membership usesin, adding usesappend(), removing usespop().Final Answer:
It returns the number of items in the list. -> Option DQuick Check:
len()counts items [OK]
- Confusing len() with in operator
- Thinking len() adds or removes items
- Mixing up list methods with functions
Which of the following is the correct syntax to check if the number 5 is in the list nums?
nums = [1, 3, 5, 7]
Solution
Step 1: Recall correct membership syntax
Python usesinto check if an item is inside a list:item in list.Step 2: Check each option
Onlyif 5 in nums:matches correct syntax; others are invalid Python.Final Answer:
if 5 in nums: -> Option CQuick Check:
Useinfor membership test [OK]
- Writing 'inside' or 'contains' instead of 'in'
- Reversing order like 'list in item'
- Using invalid keywords for membership
What will be the output of this code?
fruits = ['apple', 'banana', 'cherry']
print(len(fruits))
print('banana' in fruits)
print('orange' in fruits)Solution
Step 1: Calculate length of the list
The list has 3 items: 'apple', 'banana', 'cherry', solen(fruits)is 3.Step 2: Check membership for 'banana' and 'orange'
'banana' is in the list, so'banana' in fruitsis True. 'orange' is not, so'orange' in fruitsis False.Final Answer:
3 True False -> Option AQuick Check:
len=3, banana in list=True, orange in list=False [OK]
- Confusing True/False for membership
- Miscounting list length
- Expecting syntax error for correct code
Find the error in this code snippet:
items = [10, 20, 30]
if 20 in items
print("Found 20")Solution
Step 1: Check syntax of if statement
Python requires a colon ':' at the end of the if condition line.Step 2: Identify the missing colon
The code hasif 20 in itemswithout a colon, causing a syntax error.Final Answer:
Missing colon ':' after the if condition. -> Option AQuick Check:
if statements need ':' [OK]
- Forgetting colon after if condition
- Misusing 'in' operator syntax
- Using parentheses instead of brackets for lists
You have a list numbers = [2, 4, 6, 8]. You want to check if the list has exactly 4 items and if the number 5 is not in the list. Which code correctly does this?
Solution
Step 1: Check list length condition
Uselen(numbers) == 4to test if list has exactly 4 items.Step 2: Check membership condition
Use5 not in numbersto confirm 5 is not in the list.Step 3: Combine conditions correctly
Useandto require both conditions true together.Final Answer:
if len(numbers) == 4 and 5 not in numbers: print("Condition met") -> Option BQuick Check:
Length check and membership negation combined with and [OK]
- Using single '=' instead of '==' for comparison
- Using or instead of and for both conditions
- Checking for 5 in list instead of not in
