0
0
Pythonprogramming~20 mins

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

Choose your learning style9 modes available
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.