Extract Even-Indexed Items
Intermediate · Write the code · from Python Slicing: Master Indexing for Strings, Lists, and Tuples
From the lesson: Python Slicing: Master Indexing for Strings, Lists, and Tuples
Imagine a pizza cut into 8 slices, numbered 0 through 7. You don't always want the whole pizza. Sometimes you want slices 2 through 5. Sometimes you want every other slice. Sometimes you want the last three slices.
Read the lessonGiven the list letters, use slicing with a step to print only the items at even indices (0, 2, 4, 6). The result should be ['a', 'c', 'e', 'g'].
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
# Print items at even indices using slicing
More Slicing exercises
- Get the Middle ItemsBeginner
- Predict the Slice OutputBeginner
- Reverse a WordBeginner
- Fix the Slicing BugIntermediate
- Palindrome CheckerAdvanced
All Python exercises · 50 practice problems · Data Structures quiz