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 trialDebajyoti Kar
Front End Web Development Techdegree Student 2,244 Pointscouldn't we have just equated the two variables instead of using .copy?
I understand the purpose might have been just to share the existence of such method. But instead of using
- items = wishes.copy() could he not have just done
- items = wishes
is there any specific case where using .copy method might be more advantageous over simply equating the two variable? Or it doesn't matter and either would work.
1 Answer
Steven Parker
231,275 PointsAs Philip explained, a simple assignment causes the new variable to point to the same list. So if you then add another item to "wishes", when you examine "items" it will be there also! To make them independent requires a copy.
The other question Philip linked to has a discussion about equality vs. identity that you may also find helpful.
Philip Schultz
11,437 PointsHere is the thread about identity vs equality - https://teamtreehouse.com/community/how-does-memory-work-in-python
Steven Parker
231,275 PointsI don't think I realized until now that it was you asking both questions!
Philip Schultz
11,437 PointsYup it was me. You've helped me out a few times now......really appreciate your help.
Debajyoti Kar
Front End Web Development Techdegree Student 2,244 Pointsthanks, Steven and Philip. It makes sense. Since I have learned C and C++ first, sometimes I get confused as I am learning Python. Thanks for clarifying you guys.
Steven Parker
231,275 PointsI can relate. I was a C/C++ developer for many years before I learned Python. Wow, what a difference!
Philip Schultz
11,437 PointsI went from C to Java and now Python.... it is a little confusing. I'm currently reading this article that goes really in depth about the topic. It points out that you can think of identities in python like you did pointers in C++. Check it out, it is a good read. https://www.blog.pythonlibrary.org/2017/02/28/python-101-equality-vs-identity/
JASON LEE
17,352 PointsIt seems this "point" behavior applies if the new variable is assigned to another variable that is of type list. It does not seem to apply if the variable is assigned to another variable that is of type int or string. For instance with the following declaration ... A = 5 B = A ... Any subsequent change to A won't affect the value in B, and vice-versa. Whereas with this declaration ... A = ["Alpha", "Bravo", "Charlie"] B = A ... Any subsequent change in the list (i.e. push, append, extend) by referencing either variable A or B will impact both variables. Is this correct?
Philip Schultz
11,437 PointsPhilip Schultz
11,437 PointsHey, Here is a link to a conversation I had the other day. Steven explained to me why we have to use .copy() and not the equal sign. Basically, if you use the equal sign you are just making another reference to the same list. You want to make a copy of the list to accomplish most things. Read the entire thread! https://teamtreehouse.com/community/cant-use-remove-to-remove-the-list-from-inside-of-messylist-why