This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: append() works not correct
Type: behavior Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: merli, remi.lapeyre
Priority: normal Keywords:

Created on 2020-03-31 09:24 by merli, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg365371 - (view) Author: (merli) Date: 2020-03-31 09:24
Please try out and look bug in this example:

Liste   = []
StringL = ["Nr.:", "NR", "Bielefeld", "Paderborn", "Lemgo"]
for i in range (10):
    StringL[1] = str(i)
    Liste.append(StringL)
    print (StringL)
    #print (Liste)
    print ()

print()
for i in range (10):
    print (Liste[i])
msg365372 - (view) Author: (merli) Date: 2020-03-31 09:32
Output:

['Nr.:', '9', 'Bielefeld', 'Paderborn', 'Lemgo']
['Nr.:', '9', 'Bielefeld', 'Paderborn', 'Lemgo']
['Nr.:', '9', 'Bielefeld', 'Paderborn', 'Lemgo']
['Nr.:', '9', 'Bielefeld', 'Paderborn', 'Lemgo']
['Nr.:', '9', 'Bielefeld', 'Paderborn', 'Lemgo']
['Nr.:', '9', 'Bielefeld', 'Paderborn', 'Lemgo']
['Nr.:', '9', 'Bielefeld', 'Paderborn', 'Lemgo']
['Nr.:', '9', 'Bielefeld', 'Paderborn', 'Lemgo']
['Nr.:', '9', 'Bielefeld', 'Paderborn', 'Lemgo']
['Nr.:', '9', 'Bielefeld', 'Paderborn', 'Lemgo']
msg365373 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-03-31 10:05
Hi merli, there is no bug here, you always append the same list to Liste so when modifying it you override previous values. You could make a copy of it by using `Liste.append(list(StringL))` instead of `Liste.append(StringL)` and you would get the behaviour you expect.


Please ask questions in python-help or in StackOverflow for questions related to Python usage.
History
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84304
2020-03-31 10:11:18steven.dapranosetstatus: open -> closed
resolution: not a bug
stage: resolved
2020-03-31 10:05:20remi.lapeyresetnosy: + remi.lapeyre
messages: + msg365373
2020-03-31 09:32:36merlisetmessages: + msg365372
2020-03-31 09:24:14merlicreate