Bird
Raised Fist0
Pythonprogramming~10 mins

List length and membership test in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - List length and membership test
Start with a list
Check length with len()
Use 'in' to test membership
Return length and membership result
We start with a list, then find its length using len(), and check if an item is in the list using 'in'. Finally, we get the results.
Execution Sample
Python
my_list = [2, 4, 6, 8]
length = len(my_list)
has_six = 6 in my_list
print(length, has_six)
This code finds the length of the list and checks if 6 is inside it, then prints both results.
Execution Table
StepActionExpression EvaluatedResultOutput
1Create listmy_list = [2, 4, 6, 8][2, 4, 6, 8]
2Calculate lengthlen(my_list)4
3Check membership6 in my_listTrue
4Print resultsprint(length, has_six)4 True
💡 All steps completed, program ends after printing.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
my_listundefined[2, 4, 6, 8][2, 4, 6, 8][2, 4, 6, 8][2, 4, 6, 8]
lengthundefinedundefined444
has_sixundefinedundefinedundefinedTrueTrue
Key Moments - 2 Insights
Why does len(my_list) return 4 instead of the last number in the list?
len(my_list) counts how many items are in the list, not the value of any item. See step 2 in execution_table where len(my_list) is 4 because there are 4 items.
Why does '6 in my_list' return True even though 6 is not the first item?
'in' checks every item in the list until it finds 6. It doesn't matter where 6 is. Step 3 shows '6 in my_list' is True because 6 is present.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value of length?
A8
B4
C6
DUndefined
💡 Hint
Check the 'Result' column at step 2 in execution_table.
At which step does the program check if 6 is in the list?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for membership test.
If we check '5 in my_list' instead of '6 in my_list', what would be the result at step 3?
AError
BTrue
CFalse
DUndefined
💡 Hint
Since 5 is not in the list, membership test returns False.
Concept Snapshot
List length and membership test in Python:
- Use len(list) to get number of items.
- Use 'item in list' to check if item exists.
- len() returns an integer count.
- 'in' returns True or False.
- Both are simple and fast ways to inspect lists.
Full Transcript
This example shows how to find the length of a list and check if a value is inside it. First, we create a list with four numbers. Then, we use len() to count how many items are in the list, which is 4. Next, we check if the number 6 is in the list using '6 in my_list', which returns True because 6 is present. Finally, we print both results: 4 and True. This helps us understand how to quickly get list size and test membership in Python.

Practice

(1/5)
1.

What does the len() function do when used with a list in Python?

easy
A. It removes the last item from the list.
B. It checks if an item is in the list.
C. It adds a new item to the list.
D. It returns the number of items in the list.

Solution

  1. Step 1: Understand the purpose of len()

    The len() function counts how many elements are inside a list.
  2. Step 2: Compare with other options

    Checking membership uses in, adding uses append(), removing uses pop().
  3. Final Answer:

    It returns the number of items in the list. -> Option D
  4. Quick Check:

    len() counts items [OK]
Hint: Remember: len() counts items, in checks presence [OK]
Common Mistakes:
  • Confusing len() with in operator
  • Thinking len() adds or removes items
  • Mixing up list methods with functions
2.

Which of the following is the correct syntax to check if the number 5 is in the list nums?

nums = [1, 3, 5, 7]
easy
A. if 5 inside nums:
B. if nums in 5:
C. if 5 in nums:
D. if nums contains 5:

Solution

  1. Step 1: Recall correct membership syntax

    Python uses in to check if an item is inside a list: item in list.
  2. Step 2: Check each option

    Only if 5 in nums: matches correct syntax; others are invalid Python.
  3. Final Answer:

    if 5 in nums: -> Option C
  4. Quick Check:

    Use in for membership test [OK]
Hint: Use 'item in list' to check membership [OK]
Common Mistakes:
  • Writing 'inside' or 'contains' instead of 'in'
  • Reversing order like 'list in item'
  • Using invalid keywords for membership
3.

What will be the output of this code?

fruits = ['apple', 'banana', 'cherry']
print(len(fruits))
print('banana' in fruits)
print('orange' in fruits)
medium
A. 3\nTrue\nFalse
B. 3\nFalse\nTrue
C. Error\nTrue\nFalse
D. 3\nTrue\nTrue

Solution

  1. Step 1: Calculate length of the list

    The list has 3 items: 'apple', 'banana', 'cherry', so len(fruits) is 3.
  2. Step 2: Check membership for 'banana' and 'orange'

    'banana' is in the list, so 'banana' in fruits is True. 'orange' is not, so 'orange' in fruits is False.
  3. Final Answer:

    3 True False -> Option A
  4. Quick Check:

    len=3, banana in list=True, orange in list=False [OK]
Hint: Count items with len(), check presence with in [OK]
Common Mistakes:
  • Confusing True/False for membership
  • Miscounting list length
  • Expecting syntax error for correct code
4.

Find the error in this code snippet:

items = [10, 20, 30]
if 20 in items
    print("Found 20")
medium
A. Missing colon ':' after the if condition.
B. Wrong use of 'in' operator.
C. List should be defined with parentheses, not brackets.
D. print statement syntax is incorrect.

Solution

  1. Step 1: Check syntax of if statement

    Python requires a colon ':' at the end of the if condition line.
  2. Step 2: Identify the missing colon

    The code has if 20 in items without a colon, causing a syntax error.
  3. Final Answer:

    Missing colon ':' after the if condition. -> Option A
  4. Quick Check:

    if statements need ':' [OK]
Hint: Always put ':' after if condition [OK]
Common Mistakes:
  • Forgetting colon after if condition
  • Misusing 'in' operator syntax
  • Using parentheses instead of brackets for lists
5.

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?

hard
A. if len(numbers) = 4 and 5 not in numbers: print("Condition met")
B. if len(numbers) == 4 and 5 not in numbers: print("Condition met")
C. if len(numbers) == 4 or 5 in numbers: print("Condition met")
D. if len(numbers) == 4 and 5 in numbers: print("Condition met")

Solution

  1. Step 1: Check list length condition

    Use len(numbers) == 4 to test if list has exactly 4 items.
  2. Step 2: Check membership condition

    Use 5 not in numbers to confirm 5 is not in the list.
  3. Step 3: Combine conditions correctly

    Use and to require both conditions true together.
  4. Final Answer:

    if len(numbers) == 4 and 5 not in numbers: print("Condition met") -> Option B
  5. Quick Check:

    Length check and membership negation combined with and [OK]
Hint: Use 'len() == number' and 'item not in list' with and [OK]
Common Mistakes:
  • Using single '=' instead of '==' for comparison
  • Using or instead of and for both conditions
  • Checking for 5 in list instead of not in