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

help

fill in the blank

Copy the entire list with a slice.

a_list = [1, 2, 3]

my code:

a_list=[1:3] [2, 3]

21 Answers

Arrays start with an index of 0. Also, the first number in the slice is inclusive while the second number is exclusive. So the proper slice would be

a_list[0:3]

Hmm, I tried all of those things and I'm still getting "Incorrect"

As Nicholas Olsen said, index starts at 0 and goes upto len()-1.

You can also slice list with following syntax. By just adding starting index, you are explicitly telling that take from starting index to all the way up to len(a_list)-1

a_list[0:]

Other way would be just specifying the end index. In the below syntax, you are just specifying ending index meaning you are telling I want all the element from starting index (default 0) to all the way up to 3.

a_list[:3]

I hope it helps. If you have any questions please feel free to ask here.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

a_list[:] works too because the 0 is implied.

But doesn't it create a copy of a_list? Does copy of a list variable equal list variable?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Yes, a_list[:] creates a copy of the list variable. But a_list[0:] also creates a copy of the list. In fact, they create the exact same copy of the list.

(To be 100% accurate, though, neither creates a copy of the list. Rather they create a slice from the list that happens to be 100% of the list.)

i need help my code is

favorite_things = ['raindrops on roses', 'whiskers on kittens', 'bright copper kettles', 'warm woolen mittens', 'bright paper packages tied up with string', 'cream colored ponies', 'crisp apple strudels']

slice1 = [1, 2, 3] slice1[:3]

and on top of that i tried all of your answers

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

This thread relates to the old version of the course. What problem are you having?

a_list = [1, 2, 3]

a_list = a_list[:]

Correct?

Thanks. I missed the subtlety of that. (do'h)

slice1 = [1, 2, 3] slice1[0:3]

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Hmm, no. That makes a list named slice1 and then makes a slice that has the entire contents of it in it. This is a great thing to try out in your Python shell

but it says to make a varible named slice1

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

It says

Create a new variable named slice1 that has the second, third, and fourth items from favorite_things.

So, yes, you make a variable named slice1 but it doesn't say that slice1 is a list or that it should have the numbers 1, 2, and 3 in it.

is'nt it slice1 = favorite_things[1, 2, 3]

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

favorite_things[1, 2, 3] isn't the slice syntax. Slices use a colon.

or slice1 = [1, 2, 3]

so like favorite_things[1:, 2:, 3:]?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Maybe instead of guessing, go watch the video again. There's no shame in rewatching content

ok

it's confusing

hardest challenge i went through

so far

kenneth i watched the video twice and still very confusing

Ken Alger
Ken Alger
Treehouse Teacher

Isaac;

You are using this as your list, correct?

favorite_things = ['raindrops on roses', 'whiskers on kittens', 'bright copper kettles', 'warm woolen mittens', 'bright paper packages tied up with string', 'cream colored ponies', 'crisp apple strudels']

And the task calls for us to utilize the slice syntax and put the 2nd, 3rd, and 4th items into a new variable called slice1, correct?

So we would need to do something for:

slice1= <some slice syntax here>

In watching the videos, how do we slice lists in Python? If we have a list and want to capture the fifth, sixth, and seventh items in the list we would do our_list[4:7], correct? The 4 representing where we want to start our slice and the 7 representing the item after we want to stop, therefore, in this example we would be capturing items 5, 6, and 7.

Our task then, is to apply that same concept and similar syntax to this task.

The concept of slicing lists is an important one to have a good handle on and it is great that you are working your way through it and asking questions! Post back if you are still stuck and feel free to ask additional questions.

Ken

Guys,

This is getting frustrating ...

I gave the answer [:] (without a leading space!) and it failed me the first two times. On the third try it worked.

And the previous exercises can't be completed for this very same reason. Even copy pasting the correct answer from help section does not work . (Slice functions question 2 fails regardless ...)

Please check few more things:

  • Did you spell the variable name correctly,
  • There is no space between variable name and [], for example a_list[:]
  • There is colon between inside the square brackets.

I hope this helps and if you does not work after all this please post your code here.

Ron Kopald Thanks for your code. All you have to do is just fill the blank with the [](square brackets) with slice that copies the entire list.

You do not need to assign the value the slice to a_list variable.

So basically, fill blank with [:]. You will be fine. I hope it helps.

a_list = [1, 2, 3]
a_list[:3]

Kenneth i typed in the following answers and they are not working

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

slice1 should have the 2nd, 3rd, and 4th items from favorite_things. Lists, which favorite_things is, are 0-indexed, so the first item has index of 0. What would be the indexes for items 2, 3, and 4?