Python Tip of the Day: Advanced List Slicing with Steps
Advanced List Slicing with Steps
Author: Jeremy Morgan
Published: November 30, 2024
I wrote a book! Check out A Quick Guide to Coding with AI.
Become a super programmer!
Learn how to use Generative AI coding tools as a force multiplier for your career.
You can slice lists with steps to skip elements—super useful!
# Slicing with steps
numbers = [0, 1, 2, 3, 4, 5, 6]
print(numbers[::2]) # Output: [0, 2, 4, 6]
print(numbers[::-1]) # Output: [6, 5, 4, 3, 2, 1, 0]
The Python Tip of the Day is a daily series published in the month of November. The tips are designed to help you become a better Python programmer. I post tips like this and more every single day on X. Let’s connect!
I wrote a book! Check out A Quick Guide to Coding with AI.
Become a super programmer!
Learn how to use Generative AI coding tools as a force multiplier for your career.