Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Slices Slice Functions

Emilio Andere
Emilio Andere
495 Points

What does as a single value refer to?

does it mean to put it 's [:1], I don't get it!

slices.py
def first_4(list1):
    return list1[:4]

first_4([2, 7, 9, 4, 6])

def first_and_last_4(list2)
    return list2

1 Answer

Manish Giri
Manish Giri
16,266 Points

When slicing, you can indicate a start and end position, so my_list[1:5] will get you the elements at indices 1 through 4.

If either start or end is omitted, it is assumed to be the start, or end of the list respectively. So-

my_list[1:] # gets you the elements from index 1 till the end of the list

my_list[:5] # gets you the elements from the start of the list(index 0) till index 4