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
38 views

1 answer to this question.

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,900 points
32 views
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,900 points
28 views
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
161 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 282 views
+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
568 views
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
81 views