Bird
Raised Fist0
Pythonprogramming~10 mins

List indexing and slicing 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 indexing and slicing
Start with a list
Choose index or slice
If index: get single element
If slice: get sublist
Return result
End
You start with a list, pick an index or a slice, then get either one element or a sublist returned.
Execution Sample
Python
my_list = [10, 20, 30, 40, 50]
print(my_list[1])
print(my_list[1:4])
This code gets the element at index 1 and a slice from index 1 up to 4 (not including 4).
Execution Table
StepExpressionEvaluationResultExplanation
1my_list[1]Index 120Get element at index 1 (second element)
2my_list[1:4]Slice from 1 to 3[20, 30, 40]Get elements from index 1 up to 4 (excluding 4)
3my_list[-1]Index -150Get last element using negative index
4my_list[:3]Slice from start to 2[10, 20, 30]Get first three elements
5my_list[3:]Slice from 3 to end[40, 50]Get elements from index 3 to end
6my_list[::2]Slice with step 2[10, 30, 50]Get every second element starting at index 0
7my_list[4:1:-1]Slice backwards[50, 40, 30]Get elements from index 4 down to 2 in reverse
8my_list[5]Index 5ErrorIndexError: index out of range, list has 5 elements (0-4)
9my_list[1:10]Slice from 1 to 9[20, 30, 40, 50]Slice stops at list end, no error
💡 IndexError occurs if index is out of range; slices gracefully handle out-of-range end.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9
my_list[10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50]
Key Moments - 3 Insights
Why does my_list[1:4] include index 1 but not index 4?
Because slicing in Python includes the start index but excludes the end index, so it takes elements at 1, 2, and 3 only (see execution_table row 2).
What happens if I use a negative index like my_list[-1]?
Negative indexes count from the end, so -1 means the last element (see execution_table row 3).
Why does my_list[5] cause an error but my_list[1:10] does not?
Indexing with 5 is out of range and causes an error, but slicing stops at the list end without error (see execution_table rows 8 and 9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of my_list[3:] at step 5?
A[40, 50]
B[30, 40]
C[10, 20, 30]
D[50]
💡 Hint
Check the 'Result' column in execution_table row 5.
At which step does an IndexError occur?
AStep 2
BStep 8
CStep 4
DStep 9
💡 Hint
Look for 'Error' in the 'Result' column in execution_table.
If we change my_list[1:4] to my_list[1:5], what would be the result?
A[20, 30, 40]
B[10, 20, 30, 40]
C[20, 30, 40, 50]
DIndexError
💡 Hint
Slicing includes start index but excludes end index; see execution_table row 9 for similar slice.
Concept Snapshot
List indexing: my_list[index] gets one element.
Negative index counts from end (-1 last).
Slicing: my_list[start:end] gets sublist from start up to but not including end.
Omitting start or end means from start or to end.
Slices handle out-of-range gracefully; indexing does not.
Use step in slicing: my_list[start:end:step].
Full Transcript
This lesson shows how Python lists let you get single elements or parts of the list using indexing and slicing. Indexing uses a single number inside square brackets to get one element. Negative indexes count from the end. Slicing uses two numbers separated by a colon to get a sublist from the start index up to but not including the end index. You can omit start or end to slice from the beginning or to the end. Slices do not cause errors if the end is beyond the list length, but indexing does. You can also add a step to slice every nth element or reverse the list. The execution table shows examples and results step by step.

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