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 First Slice

What kind of question is this?

?

3 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

actually-- the only thing you are supposed to type in is [:]

It stumped me the first time too. I feel they left out the '=' sign. I noted the issue to the Treehouse staff so at some point it will be changed to be similar to what you typed above.

Good luck with your Python journey!!

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

Are you referring to the slice copy question? That's the only question that is slightly odd.

Since a list is "mutable" you have to make sure your copy a list if you want to keep it intact. As an example, if I take a list x and check its id() then I assign y equal to x, then I check its id(). You will notice the ids are the same (they are the same list!) so if I change y[0] = 'Hello', you will see x[0] is also 'Hello'

Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = [1,2,3,4]
>>> x
[1, 2, 3, 4]
>>> id(x)
1416993727112
>>> y = x
>>> id(y)
1416993727112
>>> y[0] = 'Hello'
>>> x
['Hello', 2, 3, 4]

But, we can copy a list with a slice y=x[:] it looks strange, but accomplishes creating a COPY of the list (with an entirely different memory id). Now... when we change y[0] = 'Hello', notice that x is NOT CHANGED.

Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = [1,2,3,4]
>>> id(x)
2777124397704
>>> y = x[:]
>>> id(y)
2777124886984
>>> y[0] = 'Hello'
>>> y
['Hello', 2, 3, 4]
>>> x
[1, 2, 3, 4]

I'm refering to that question and I know the answer, but it does not work and it does not make sense.

It says copy this list for e.g.

X = [1,2,3,4,5]

X = __________

So it wants me to make it look like X = X[:] ??

Thank you Jeff!