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

first_and_last_4

I'm not sure how to add two iterations together, so I took a stab at it but I don't know what I'm doing incorrectly. Hopefully I'm on the right track, but I have no idea.

slices.py
def first_4(iterable):
    first_4 = iterable[0:4]
    return first_4

def first_and_last_4(iterable):
    first_and_last_4 = iterable[0:4] + iterable[-1:-5:-1]

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

You are headed in the right direction. When working with slices, the second perimeter stop must be after the first parameter start. In your code above, the -1 comes after the -5. This results in an empty list that is then reversed with the -1 step parameter, which is still an empty list.

If you want to go from any point to the end of the list, leave the stop parameter empty. Using this idea, what would be a good starting point to get the last four items?

Yes, using the + to add two iterables is valid. If both are strings than a string is returned if both are lists a new list is returned.

Post back if you need more help. Good luck!!!

I figured it out last night. Thanks!

Chris, Now I have new issue with reverse_evens on the 4th challenge. To figure this out I went into interactive mode in the shell:

python
treehouse:~/workspace$ python -i                                                                                                                               
Python 3.6.4 (default, Apr 18 2019, 20:59:32)                                                                                                                  
[GCC 5.4.0 20160609] on linux                                                                                                                                  
Type "help", "copyright", "credits" or "license" for more information.                                                                                         
>>> iterable = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]                                                                                                             
>>> iterable[::2]                                                                                                                                              
[1, 3, 5, 7, 9, 11]                                                                                                                                            
>>> even_indices = iterable[::2]                                                                                                                               
>>> even_indices                                                                                                                                               
[1, 3, 5, 7, 9, 11]                                                                                                                                            
>>> even_indices[::-1]                                                                                                                                         
[11, 9, 7, 5, 3, 1]                                                                                                                                            

I then used this for my function; however, it's not working and I don't know why.

def reverse_evens(iterable):
    even_indices = iterable[::2]
    return even_indices[::-1]

Weird....I just copied and pasted and it worked now. Literally two seconds ago it didn't.

Problem solved. :)

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Great! Way to keep at it. Bonus comment:

You can take a slice of a slice by appending another slice notation:

>>> [1,2,3,4,5,6,7,8,9,10,11,12]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> [1,2,3,4,5,6,7,8,9,10,11,12][1::2]
[2, 4, 6, 8, 10, 12]
>>> [1,2,3,4,5,6,7,8,9,10,11,12][1::2][::3]
[2, 8]

Oh cool! I love this stuff! Growth mindset! :) Thanks again!