Bird
Raised Fist0
Pythonprogramming~20 mins

List indexing and slicing in Python - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
๐ŸŽ–๏ธ
List Slicing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of negative step slicing
What is the output of this Python code?
lst = [10, 20, 30, 40, 50]
print(lst[4:1:-1])
Python
lst = [10, 20, 30, 40, 50]
print(lst[4:1:-1])
A[50, 40, 30, 20]
B[50, 40, 30]
C[40, 30, 20]
D[50, 40]
Attempts:
2 left
๐Ÿ’ก Hint
Remember that slicing with a negative step goes backwards and excludes the stop index.
โ“ Predict Output
intermediate
2:00remaining
Indexing with out-of-range positive index
What happens when you run this code?
lst = [1, 2, 3]
print(lst[5])
Python
lst = [1, 2, 3]
print(lst[5])
AIndexError
B0
CNone
D3
Attempts:
2 left
๐Ÿ’ก Hint
Check what happens if you try to access an index that does not exist.
โ“ Predict Output
advanced
2:00remaining
Slicing with omitted start and stop
What is the output of this code?
lst = ['a', 'b', 'c', 'd', 'e']
print(lst[::-2])
Python
lst = ['a', 'b', 'c', 'd', 'e']
print(lst[::-2])
A['e', 'd', 'c', 'b', 'a']
B['a', 'c', 'e']
C['e', 'd', 'b']
D['e', 'c', 'a']
Attempts:
2 left
๐Ÿ’ก Hint
When start and stop are omitted with a negative step, slicing goes from the end to the start.
โ“ Predict Output
advanced
2:00remaining
Effect of slice assignment on list
What is the value of lst after this code runs?
lst = [1, 2, 3, 4, 5]
lst[1:4] = [9, 8]
print(lst)
Python
lst = [1, 2, 3, 4, 5]
lst[1:4] = [9, 8]
print(lst)
A[1, 9, 8, 3, 4, 5]
B[1, 9, 8, 5]
C[1, 9, 8, 4, 5]
D[1, 2, 9, 8, 5]
Attempts:
2 left
๐Ÿ’ก Hint
Slice assignment replaces the elements in the slice with the new list, adjusting length if needed.
๐Ÿง  Conceptual
expert
3:00remaining
Understanding slice indices with negative steps
Given the list lst = [0,1,2,3,4,5,6,7,8,9], what is the output of lst[7:2:-2]?
Python
lst = [0,1,2,3,4,5,6,7,8,9]
print(lst[7:2:-2])
A[7, 5, 3]
B[7, 6, 5, 4, 3]
C[7, 5, 4, 3]
D[7, 6, 4, 2]
Attempts:
2 left
๐Ÿ’ก Hint
Remember slicing excludes the stop index and steps backwards by 2.

Practice

(1/5)
1. What does the expression my_list[2] return when my_list = [10, 20, 30, 40, 50]?
easy
A. 40
B. 20
C. 30
D. 50

Solution

  1. Step 1: Understand list indexing

    Indexing starts at 0, so my_list[0] is 10, my_list[1] is 20, and my_list[2] is 30.
  2. Step 2: Identify the value at index 2

    The value at index 2 is 30.
  3. Final Answer:

    30 -> Option C
  4. Quick Check:

    Index 2 value = 30 [OK]
Hint: Remember indexing starts at zero, so count from 0 [OK]
Common Mistakes:
  • Starting count from 1 instead of 0
  • Confusing index 2 with index 3
  • Mixing up values and indexes
2. Which of the following is the correct syntax to get the last element of a list nums using indexing?
easy
A. nums[last]
B. nums[1]
C. nums[len(nums)]
D. nums[-1]

Solution

  1. Step 1: Recall negative indexing

    Negative indexes count from the end, so -1 means the last element.
  2. Step 2: Check each option

    nums[-1] uses nums[-1], which correctly accesses the last element. nums[1] accesses the second element. nums[last] is invalid syntax. nums[len(nums)] causes an IndexError because list indexes go from 0 to len(nums)-1.
  3. Final Answer:

    nums[-1] -> Option D
  4. Quick Check:

    Last element index = -1 [OK]
Hint: Use -1 to get the last item in a list [OK]
Common Mistakes:
  • Using len(nums) as index (out of range)
  • Trying to use 'last' as an index
  • Confusing positive and negative indexes
3. What is the output of this code?
letters = ['a', 'b', 'c', 'd', 'e']
print(letters[1:4])
medium
A. ['b', 'c', 'd']
B. ['c', 'd', 'e']
C. ['b', 'c', 'd', 'e']
D. ['a', 'b', 'c']

Solution

  1. Step 1: Understand slicing syntax

    Slicing letters[1:4] means start at index 1 up to but not including index 4.
  2. Step 2: Identify elements at indexes 1, 2, 3

    Index 1 is 'b', index 2 is 'c', index 3 is 'd'. So the slice is ['b', 'c', 'd'].
  3. Final Answer:

    ['b', 'c', 'd'] -> Option A
  4. Quick Check:

    Slicing excludes stop index [OK]
Hint: Slice stops before the stop index, not including it [OK]
Common Mistakes:
  • Including the stop index element
  • Confusing start and stop indexes
  • Using wrong indexes for slicing
4. The code below causes an error. What is the problem?
nums = [1, 2, 3, 4, 5]
print(nums[5])
medium
A. SyntaxError due to wrong brackets
B. IndexError because index 5 is out of range
C. TypeError because nums is not a list
D. No error, prints 5

Solution

  1. Step 1: Check list length and valid indexes

    List nums has 5 elements with indexes 0 to 4.
  2. Step 2: Understand index 5 usage

    Index 5 is outside the valid range, so accessing nums[5] causes an IndexError.
  3. Final Answer:

    IndexError because index 5 is out of range -> Option B
  4. Quick Check:

    Index must be less than list length [OK]
Hint: Indexes go from 0 to length-1 only [OK]
Common Mistakes:
  • Thinking index 5 is valid for 5 elements
  • Confusing SyntaxError with IndexError
  • Assuming list indexes start at 1
5. Given data = [5, 10, 15, 20, 25, 30], which expression returns a new list with every second element in reverse order?
hard
A. data[::-2]
B. data[::2]
C. data[-2::-1]
D. data[1::2]

Solution

  1. Step 1: Understand slicing with step and negative step

    The slice data[::-2] starts from the end and takes every second element backwards.
  2. Step 2: Check what data[::-2] returns

    It returns [30, 20, 10], which is every second element in reverse order.
  3. Step 3: Verify other options

    data[::2] returns every second element forward: [5, 15, 25]. data[-2::-1] returns from index -2 backwards: [25, 20, 15, 10, 5]. data[1::2] returns every second element starting at index 1: [10, 20, 30].
  4. Final Answer:

    data[::-2] -> Option A
  5. Quick Check:

    Negative step reverses and skips elements [OK]
Hint: Use negative step to reverse and skip elements [OK]
Common Mistakes:
  • Using positive step for reverse
  • Confusing start index with step sign
  • Misunderstanding slice boundaries