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 trialJosh White
3,630 PointsSlicing in python
Im having trouble with this challenge. Maybe I am simply misunderstanding what it is asking me to do I am not sure. The problem is to return the first 4 to whatever is sent to it. I tried it by turning the item into a list and looping through it. I've tried using the the index for example print(item[:3]) but i just cant figure out what I am doing wrong.
def first_4(items):
listItem = list(items)
i = 0
while i < 4:
print(listItem[i])
i += 1
1 Answer
Steven Parker
231,248 PointsThe value of a slice is that you won't need a loop. You had the right idea with "item[:3]
" but a few issues:
- the parameter name is not "item" but "items" (plural)
- you want the first four items instead of the first three (the 2nd number is a limit not an index)
- you don't need to "print" anything, but you need to return the result