0

This code changes anum but it should not?? What is going wrong?
Many thanks

>>> astr=
>>> anum=[4,3,2,1]
>>> anew=anum
>>> for c,d in enumerate(anum):
... anew[c]=astr[c]
... print(anew,astr,anum)
...


>>>

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by orangepyth
0

This code changes anum but it should not?? What is going wrong?
Many thanks

>>> astr=
>>> anum=[4,3,2,1]
>>> anew=anum
>>> for c,d in enumerate(anum):
... anew[c]=astr[c]
... print(anew,astr,anum)
...


>>>

Use anew = list(anum) to make a copy, otherwise anew is only an alias for anum.

Edited by Gribouillis: n/a

0

Use anew = list(anum) to make a copy, otherwise anew is only a alias for anum.

Thank you very much.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.