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

Gilang Ilhami
Gilang Ilhami
12,045 Points

sorting slices

I think i got the logic wrong here The lsit ave to show every third item , bakwards, from messy_list

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 = clean_list[0::3:-1]

1 Answer

So first I will say be sure you read the prompt carefully on which list it is asking you to slice. It should be messy_list again not clean_list.

You can think of slicing kind of list this.

some_list[<start>:<stop>:<step>]

START - where the list will start slicing from, starting point. It will include this item. (inclusive)

STOP - the list will stop here, will not include this item in the list. (exclusive)

STEP - it hops/skips this many items, if was 2 it would skip every other item.

Examples

messy_list = [5, 2, 1, 3, 4, 7, 8, 0, 9, -1]
#            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] index of messy_list


# -------- Example 1 ------------
# Make a new list, slicing messy_list

new_list = messy_list[0:4:1]

# --- This would read ---
# START at index 0, which is 5 in messy_list
# We will move forward 1 STEP which gets us to index 1. Which is 2 in messy_list
# We will repeat this until we hit the STOP index, which is 4. Which is 3 in messy_list
# When we hit last item, we return this sliced list.


# -------- Example 2 ------------
another_list = messy_list[::-2]

# Because we didnt pass START or STOP, It will go through the whole list.
# BUT, because we passed -2 as the STEP.
# It will go through the whole list, going backwards. 2 items at a time.
# it would return HALF the list, in backwards order.

Hope that helps?