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)
...


>>>

Recommended Answers

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 …

Jump to Post

All 2 Replies

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.

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

Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.