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

Kimmo Ojala
seal-mask
.a{fill-rule:evenodd;}techdegree
Kimmo Ojala
Python Web Development Techdegree Student 8,257 Points

Slicing trouble

My code seems to work but the tester replies that my code did not return the right values. I tested the code in workspace and it worked there. What is wrong with my code?

BR, Kimmo

PS I posted a question earlier related to a very similar situation in another coding challenge

slices.py
def first_4(iterable):
    List = list(iterable)
    Slice = List[0:4]
    return ''.join(Slice)

1 Answer

Jaxon Gonzales
Jaxon Gonzales
3,562 Points

Hi, Kimmo!

1 thing that you could change:

āž” Slices are basically parts of some object, so as long as the object you are slicing has an order (is iterable) you can slice it. This means you can slice lists, tuples and even strings! (You'll learn about tuples in just a few more courses). Anyway, here are a few examples.

str1 = "apple"

str1[0:2] # This will give me "ap"

list1 = ["hello", "my", "name", "is", "John"]

list1[0:4] # This will give me ["hello", "my", "name", "is"]

What does this mean?

This means that there is no need to convert the argument into a list. As long as the user passes in an iterable the function will work just fine!

Hope This Helps! If it does click the Up arrow!

-Jaxon