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 Sorting Slices

Stage 2 Slices Coding Challenge Error

I'm getting an error on task 2 of this challenge.

My code is as follows:

clean_list = messy_list


I get the error "Bummer! Don't change messy_list" I don't see anywhere in my code where messy_list is being changed, and I am frustrated by this error. Please help.

1 Answer

Joseph Kato
Joseph Kato
35,340 Points

Hi Lauren,

The statement clean_list = messy_list is assigning a reference to clean_list (not an independent copy of messy_list).

For instance:

>>> messy_list = [5, 2, 1, 3, 4, 7, 8, 0, 9, -1]
>>> clean_list = messy_list
>>> clean_list.append("I'm a new element")
>>> messy_list
[5, 2, 1, 3, 4, 7, 8, 0, 9, -1, "I'm a new element"]

As you can see, even though I only added an element to clean_list, the addition was also represented in messy_list.

What you want to do is assign a copy of messy_list to clean_list using slice syntax:

clean_list = messy_list[:]

Thanks, I new it was going to be a simple mistake I was making.