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

Yeeka Yau
Yeeka Yau
7,410 Points

first_and_last_4 challenge

Hi Everyone, just started with python, and struggling a bit with this challenge.

I ran my function line by line in the python console in workspaces, and it works. But when I do the same by creating a python script I get a syntax error that doesn't tell me very much.

Could someone tell me where I am going wrong?

Thanks!

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

def odds(arg):
   return arg[1::2]

def first_and_last_4(alist):
  first_4 = list(alist[:4])
  last_4 = list(alist[len(my_list)-5:len(my_list)-1])
  return first_4 + last_4

5 Answers

Try this:

last_4 = a_list[-4:]
Yeeka Yau
Yeeka Yau
7,410 Points

oh thanks, that worked. But wondering why my version didn't work?

The syntax error is due to the fact that you a re probably using python3, which requires parenthesis around the parameters you send to the print function. Try it like this:

print(list_final)

Hope this helps.

Yeeka Yau
Yeeka Yau
7,410 Points

ahh ok. thanks - yes that works in the console - however, it still does not pass the challenge quiz, its saying it is returning [1,2,3,4,6,7,8,9] rather than [1,2,3,4,46,47,48,49]?

Hi,

I have copy/pasted your script in a local file on my computer and ran it. It does not give my any syntax error.

Can you post the exact message you see when you run the script on your end?

Thanks.

Yeeka Yau
Yeeka Yau
7,410 Points

Hi Andrei, thanks for your help:

I wrote the script:

my_list = list(range(40))

def first_and_last_4(a_list):
  first_4 = list(a_list[:4])
  last_4 = list(a_list[len(my_list)-5:len(my_list)-1])
  return first_4 + last_4

list_final = first_and_last_4(my_list)

print list_final

Tried to run it in workspaces, and got this error:

File "list.py", line 10
print list_final
^
SyntaxError: invalid syntax

You have what is called an "off-by-one" error. That like should look like:

last_4 = list(a_list[len(my_list)-4:len(my_list)])

But I have another thing that I want to point out: in your code you use both a_list and my_list to work with the list of numbers. Coincidently these are the same in your code. The local a_list points to the same global my_list. But you should only use the local a_list reference in your first_and_last_4 function.

last_4 = list(a_list[len(a_list)-4:len(a_list)])
Yeeka Yau
Yeeka Yau
7,410 Points

ahh yes, thanks for catching that.

rochellegermano
rochellegermano
12,878 Points

For the last function, I got mine to work by using:

def first_and_last_4(list):
  first_4 = list[:4]
  last_4 = list[-4:]
  return first_4 + last_4