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 (Retired) Slices Slice Functions

how do i return slice [1,2,3,4,46,47,48,49]

how do i return slice [1,2,3,4,46,47,48,49]? the first 4 is [:4] the last 4 should be [46:] but i don't know how to have it all showing in one list.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Remember, indexes ([5]) and values (46) aren't the same thing.

Thanks you guys you all were a great help. I couldn't have figured it out with out you guys.

3 Answers

original_list = [1,2,3,4, ... 46,47,48,49]

new_list = original_list[:4] # = [1,2,3,4]

new_list.extend( original_list[-4:] ) # = [1,2,3,4,46,47,48,49]

Sean T. Unwin
Sean T. Unwin
28,690 Points

Hi Jordan,

With regards to code challenges, we try not to give the answer right away, but give hints, clues, and feedback in order to assist.

Thank you for your contribution, however. :)

Sean T. Unwin
Sean T. Unwin
28,690 Points

To get the last 4 you could use [-4:].

Create a new list, populate it with [:4] from the first list then extend the new list with [-4:].

I hope that helps.

Thanks you guys you all were a great help. I couldn't have figured it out with out you guys.