Bird
Raised Fist0
Pythonprogramming~10 mins

List mutability in Python - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to change the first item of the list to 10.

Python
numbers = [1, 2, 3]
numbers[0] = [1]
print(numbers)
Drag options to blanks, or click blank then click option'
Anumbers[1]
B0
C'10'
D10
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using quotes around 10 makes it a string, not an integer.
Trying to assign a value to numbers instead of numbers[0].
2fill in blank
medium

Complete the code to add the number 4 at the end of the list.

Python
numbers = [1, 2, 3]
numbers.[1](4)
print(numbers)
Drag options to blanks, or click blank then click option'
Ainsert
Bappend
Cextend
Dadd
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using insert without specifying the index.
Using extend which expects an iterable, not a single item.
3fill in blank
hard

Fix the error in the code to change the second item to 20.

Python
numbers = [5, 6, 7]
numbers[[1]] = 20
print(numbers)
Drag options to blanks, or click blank then click option'
A1
B2
C0
D3
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using index 2 which is the third item.
Using index 3 which is out of range.
4fill in blank
hard

Fill both blanks to create a new list with squares of numbers greater than 2.

Python
numbers = [1, 2, 3, 4, 5]
squares = [x[1]2 for x in numbers if x [2] 2]
print(squares)
Drag options to blanks, or click blank then click option'
A**
B%
C>
D+
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using % which is modulo, not power.
Using + which adds numbers instead of squaring.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values greater than 3.

Python
numbers = {'a': 1, 'b': 4, 'c': 5}
result = [1]: [2] for [3], v in numbers.items() if v > 3}
print(result)
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
Ck
Dv.upper()
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using v.upper() which is invalid because values are integers.
Using k instead of k.upper() for keys.

Practice

(1/5)
1. Which of the following statements about Python lists is true?
easy
A. Lists cannot be changed once created.
B. Lists can be changed after they are created.
C. Lists are immutable like strings.
D. Lists can only store numbers.

Solution

  1. Step 1: Understand list mutability

    Python lists are mutable, meaning you can change their contents after creation.
  2. Step 2: Compare options

    Lists can be changed after they are created. ("Lists can be changed after they are created.") correctly states that lists are mutable. Options A and C incorrectly claim lists cannot be changed or are immutable like strings. Lists can only store numbers. is wrong because lists can store any data type.
  3. Final Answer:

    Lists can be changed after they are created. -> Option B
  4. Quick Check:

    List mutability = True [OK]
Hint: Remember: lists are like clay, easy to reshape [OK]
Common Mistakes:
  • Confusing lists with strings (immutable)
  • Thinking lists only hold numbers
  • Believing lists cannot be changed
2. Which of the following is the correct way to change the first item of a list my_list to 10?
easy
A. my_list[0] = 10
B. my_list(0) = 10
C. my_list{0} = 10
D. my_list.set(0, 10)

Solution

  1. Step 1: Recall list item assignment syntax

    In Python, list items are accessed and assigned using square brackets and index, like my_list[0].
  2. Step 2: Check each option

    my_list[0] = 10 uses correct syntax. Options B and C use wrong brackets. my_list.set(0, 10) uses a method that does not exist for lists.
  3. Final Answer:

    my_list[0] = 10 -> Option A
  4. Quick Check:

    Use square brackets for list item assignment [OK]
Hint: Use square brackets [] to access or change list items [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Trying to use curly braces for indexing
  • Assuming lists have a set() method
3. What will be the output of this code?
numbers = [1, 2, 3]
numbers[1] = 5
print(numbers)
medium
A. [1, 5, 3]
B. [1, 2, 3]
C. [5, 2, 3]
D. Error

Solution

  1. Step 1: Understand list item update

    The code changes the item at index 1 (second item) from 2 to 5.
  2. Step 2: Check the final list

    After update, the list becomes [1, 5, 3]. The print statement outputs this list.
  3. Final Answer:

    [1, 5, 3] -> Option A
  4. Quick Check:

    Updated list = [1, 5, 3] [OK]
Hint: Index 1 means second item in list [OK]
Common Mistakes:
  • Thinking index 1 is first item
  • Expecting original list unchanged
  • Assuming code causes error
4. Find the error in this code that tries to change a list item:
my_list = [10, 20, 30]
my_list(1) = 15
print(my_list)
medium
A. Missing colon after assignment
B. List cannot be changed after creation
C. Using parentheses instead of square brackets for indexing
D. print statement syntax error

Solution

  1. Step 1: Identify indexing syntax error

    The code uses parentheses () instead of square brackets [] to access list item.
  2. Step 2: Confirm correct syntax

    List items must be accessed with square brackets, so my_list[1] = 15 is correct.
  3. Final Answer:

    Using parentheses instead of square brackets for indexing -> Option C
  4. Quick Check:

    Use [] not () for list indexing [OK]
Hint: Remember: () calls functions, [] accesses list items [OK]
Common Mistakes:
  • Using () instead of [] for list indexing
  • Thinking lists are immutable
  • Looking for syntax errors elsewhere
5. Given the list data = [5, 10, 15, 20], which code snippet correctly doubles each item in the list using mutability?
hard
A. data = [item * 2 for item in data] print(data)
B. for item in data: item = item * 2 print(data)
C. data.map(lambda x: x*2) print(data)
D. for i in range(len(data)): data[i] = data[i] * 2 print(data)

Solution

  1. Step 1: Understand mutability and iteration

    To change items in place, we must assign new values to each index in the list.
  2. Step 2: Analyze each option

    for i in range(len(data)): data[i] = data[i] * 2 print(data) updates each item by index, correctly doubling values in place.
    for item in data: item = item * 2 print(data) changes only the loop variable, not the list.
    data = [item * 2 for item in data] print(data) creates a new list and assigns it to data (not in-place mutation).
    data.map(lambda x: x*2) print(data) uses map which returns an iterator and does not modify list in place.
  3. Final Answer:

    for i in range(len(data)): data[i] = data[i] * 2 print(data) -> Option D
  4. Quick Check:

    In-place update requires index assignment [OK]
Hint: Use index to update list items for true mutability [OK]
Common Mistakes:
  • Modifying loop variable instead of list items
  • Using map without converting result
  • Replacing list instead of mutating it