Challenge - 5 Problems
List Slicing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2: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])
Attempts:
2 left
๐ก Hint
Remember that slicing with a negative step goes backwards and excludes the stop index.
โ Incorrect
The slice lst[4:1:-1] starts at index 4 (value 50), goes backwards up to but not including index 1, so it includes indices 4, 3, 2 which are 50, 40, 30.
โ Predict Output
intermediate2: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])
Attempts:
2 left
๐ก Hint
Check what happens if you try to access an index that does not exist.
โ Incorrect
Accessing lst[5] raises an IndexError because the list has only 3 elements (indices 0 to 2).
โ Predict Output
advanced2: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])
Attempts:
2 left
๐ก Hint
When start and stop are omitted with a negative step, slicing goes from the end to the start.
โ Incorrect
lst[::-2] starts from the end and takes every second element backwards: indices 4, 2, 0 โ 'e', 'c', 'a'.
โ Predict Output
advanced2: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)
Attempts:
2 left
๐ก Hint
Slice assignment replaces the elements in the slice with the new list, adjusting length if needed.
โ Incorrect
lst[1:4] covers elements at indices 1, 2, 3 (values 2, 3, 4). Replacing them with [9, 8] shortens the list by one element.
๐ง Conceptual
expert3: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])
Attempts:
2 left
๐ก Hint
Remember slicing excludes the stop index and steps backwards by 2.
โ Incorrect
Starting at index 7 (value 7), stepping backwards by 2 gives indices 7, 5, 3 (values 7, 5, 3). Index 2 is excluded.