Python Tip of the Day: Slice Lists Like a Pro

Slice Lists Like a Pro

Author: Jeremy Morgan
Published: November 20, 2024


Coding with AI

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.


Extract subsets of lists using slicing—it’s powerful and concise.

# List slicing examples
my_list = [0, 1, 2, 3, 4, 5]

print(my_list[2:5])    # Output: [2, 3, 4]
print(my_list[:3])     # Output: [0, 1, 2]
print(my_list[3:])     # Output: [3, 4, 5]
print(my_list[-2:])    # Output: [4, 5]

“Python Tip of the Day: Slice Lists Like a Pro”


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!


Coding with AI

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.