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

Why use a slice to copy list?

So I watching the video Introduction to slices.

In it he said to make a quick copy of your list you can use the following:

new_list = my_list[:]

and that makes a copy of your list into the variable new_list.

Why is that better than just writing:

new_list = my_list?

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Try this in your shell

my_list = [1, 2, 3]
new_list = my_list
new_list.append(4)
print(my_list)

What comes out?

Huh....ok. Thanks for pointing that out. Seems counter intuitive but explains a problem I was having in one of your challenges. Thank you very much!