Copying lists in python

0 votes
I have assigned a list to an older list. However, the new list changes too when I modify the older one. Why is this happening?
Apr 30, 2018 in Python by NEET

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Assigning one list to another one simply means that both the lists will now refer to the same data. This is why any modifications to one of the lists gets reflected in the other one too. If you don't want this to happen, use copy.copy()

import copy
new_list = copy.copy(old_list)
answered Apr 30, 2018 by Nietzsche's daemon
• 4,260 points

Related Questions In Python

+1 vote
1 answer

Zip lists in Python

zip takes a bunch of lists likes a: ...READ MORE

answered Aug 22, 2018 in Python by Priyaj
• 56,120 points
0 votes
1 answer

How can I print Lists in python

print is a function in Python 3.x, so ...READ MORE

answered Oct 24, 2018 in Python by Priyaj
• 56,120 points
0 votes
1 answer

Pandas dataframe with multiple lists in Python

You can do it like this import pandas ...READ MORE

answered Apr 5 in Python by Esha
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
+4 votes
6 answers
0 votes
1 answer

Difference between two lists in python

difference = list(set(list1) - set(list2)) READ MORE

answered May 24, 2018 in Python by Nietzsche's daemon
• 4,260 points
0 votes
1 answer

Using Lists and Tuples in Python

if you are familiar with C programming, ...READ MORE

answered May 29, 2018 in Python by Nietzsche's daemon
• 4,260 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.