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 Sorting Slices

Nikhil Rai
Nikhil Rai
15,391 Points

my program is not going further please help!!

Hi,

I got a new challenge in Pythom collection and I did the program (it runs and gives the right result)... however the system is not letting me move forward saying list doesn't have right items.

lists.py
messy_list = [5, 2, 1, 3, 4, 7, 8, 0, 9, -1]

# Your code goes below here
clean_list = messy_list[:]
clean_list.sort()
silly_list = messy_list[len(messy_list)+1:0:-3]

1 Answer

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Why would you want to do messy_list[len(messy_list)+1:0:-3]

When you can just do messy_list[::-3]

messy_list[len(messy_list)+1:0:-3] returns [-1, 8, 3]

messy_list[::-3] returns [-1, 8, 3, 5]

messy_list[len(messy_list)+1:0:-3] This translates to 3 steps backward from index 11 to index 1. Index 11 does not exist but in this case Python won't complain and just use last item which is -1 and since it stops before index 0 your list will look like [-1, 9, 0, 8, 7, 4, 3, 1, 2] which misses item 5.

Nikhil Rai
Nikhil Rai
15,391 Points

Ooops!! Didn't notice this simple thing .... thanks a lot for help!! Cheers