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
List Indexing and Slicing
๐ Scenario: You are organizing a small collection of your favorite fruits. You want to practice how to pick specific fruits from the list using their positions.
๐ฏ Goal: Learn how to use list indexing and slicing to select individual items and groups of items from a list.
๐ What You'll Learn
Create a list with exact fruit names
Create variables to hold specific index positions
Use list indexing to get a single fruit
Use list slicing to get a group of fruits
Print the selected fruits
๐ก Why This Matters
๐ Real World
Selecting items from a list is like picking fruits from a basket by their position. This helps when you want to work with parts of a collection.
๐ผ Career
Understanding list indexing and slicing is essential for data handling, filtering, and processing in many programming jobs.
Progress0 / 4 steps
1
Create the fruit list
Create a list called fruits with these exact items in order: 'apple', 'banana', 'cherry', 'date', 'elderberry'.
Python
Hint
Remember to use square brackets [] to create a list and separate items with commas.
2
Set index variables
Create two variables: first_index set to 0 and slice_start set to 1.
Python
Hint
Just assign the numbers 0 and 1 to the variables as shown.
3
Use indexing and slicing
Create a variable first_fruit that gets the fruit at first_index from fruits. Then create a variable some_fruits that slices fruits from slice_start up to (but not including) 4.
Python
Hint
Use square brackets with the index variable for single item, and with start:end for slicing.
4
Print the selected fruits
Print first_fruit and some_fruits each on its own line.
Python
Hint
Use two print statements, one for each variable.
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
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.
Step 2: Identify the value at index 2
The value at index 2 is 30.
Final Answer:
30 -> Option C
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
Step 1: Recall negative indexing
Negative indexes count from the end, so -1 means the last element.
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.
Slicing letters[1:4] means start at index 1 up to but not including index 4.
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'].
Final Answer:
['b', 'c', 'd'] -> Option A
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
Step 1: Check list length and valid indexes
List nums has 5 elements with indexes 0 to 4.
Step 2: Understand index 5 usage
Index 5 is outside the valid range, so accessing nums[5] causes an IndexError.
Final Answer:
IndexError because index 5 is out of range -> Option B
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
Step 1: Understand slicing with step and negative step
The slice data[::-2] starts from the end and takes every second element backwards.
Step 2: Check what data[::-2] returns
It returns [30, 20, 10], which is every second element in reverse order.
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].
Final Answer:
data[::-2] -> Option A
Quick Check:
Negative step reverses and skips elements [OK]
Hint: Use negative step to reverse and skip elements [OK]