-1

How to copy a list which is consisted of list type elements? I have used the method described in a similar question how to clone a list? But it still doesn't work, as shown in picture below output of the original code

Original code is

asd
a = [[]]
b = list(a)
for i in [1,2]:
    for j in b:
        j.append(i)
    print a

why would a change when b was changed? But I want to keep a from being changed.

Requirements: cannot import package such as copy since I met this problem in leetcode.com.


Finally, the answer is that I need a deep copy of list, that is b = [c[:] for c in a]. Notice: as @PM2Ring mentioned below at comment area, b = [c[:] for c in a] is only suited for a single level of nesting. For more complicated deep copy, look for phrase "deep copy". Thanks for all your help.

guorui
  • 871
  • 2
  • 9
  • 21
  • 3
    You're making a shallow copy. It's only copying the outer list, not the inner lists. Read the question you linked to again. Look for the phrase "deep copy". – Carcigenicate Mar 19 '19 at 12:21
  • if you print `print(a==b)` you will see that `a` and `b` are the same object. Like pointed above, you made a shallow copy. – Julia Mar 19 '19 at 12:24
  • The duplicate I linked (which is also the same post that you included in your question) features the deepcopy option as its fifth bullet point. – Arne Mar 19 '19 at 12:26
  • @Carcigenicate I just know from you that there is something called deep copy!! seems like I am still new to python.... – guorui Mar 19 '19 at 12:26
  • @Arne No, it's not exactly the same, maybe as they said I need deep copy. – guorui Mar 19 '19 at 12:29
  • @Arne Ok, thank you, I'll try deep copy first. – guorui Mar 19 '19 at 12:31
  • BTW, `[c[:] for c in a]` isn't a deep copy, but it's deep enough because `a` only has a single level of nesting. – PM 2Ring Mar 21 '19 at 09:03
  • @PM2Ring Wow, I should add this message to my question!! – guorui Mar 21 '19 at 10:51

3 Answers3

2

Using copy:

import copy

a = [[]]
b = copy.copy(a)    # b = a[:] using slicing is a lot faster
for i in [1,2]:
    for j in b:
        j.append(i)
print(a)
print(b)

Without package, using slicing:

a = [[]]
b = a[:]        # b = a.copy() Could also come in handy
for i in [1,2]:
    for j in b:
        j.append(i)
print(a)
print(b)

OUTPUT:

[[1, 2]]
[[1, 2]]
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
1
import copy

lis = [[1], [2]]

deep_list = copy.deepcopy(lis)
copy_list = copy.copy(lis)

print(deep_list is lis)
print(deep_list[0] is lis[0])
print(deep_list[1] is lis[1])

print(copy_list is lis)
print(copy_list[0] is lis[0])
print(copy_list[1] is lis[1])

In the above code deepcopy() creates a deep copy of the list so you and copy() creates a shallow copy. In case of deepcopy() the list's within the list will also be copied.

OUTPUT:

False
False
False
False
True
True

EDIT: Since you cant import library may be you can try below approach:

lis = [[1], [2]]

deep_list = []

for item in lis:
    deep_list.append(item[:])

print(deep_list is lis)
print(deep_list[0] is lis[0])
print(deep_list[1] is lis[1])

OUTPUT:

False
False
False
Thanthu
  • 4,399
  • 34
  • 43
  • 1
    Correctly, I find this answer gives my desired outputs. But combined with @DirtyBit's slicing method, here I got a simple deep copy of list:`b = [c[:] for c in a]`. – guorui Mar 19 '19 at 13:02
-2
import copy
....
b = copy.copy(a)
...
Dennis F
  • 145
  • 4
  • No, I met this problem from leetcode.com, so import new package is not allowed!! – guorui Mar 19 '19 at 12:33
  • 3
    Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – double-beep Mar 19 '19 at 16:05