[Python] Why I got un-expected result about append functin? [duplicate]

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'd like to share my question on Python code that show un-expected result. The code is,

    a = ["A", "B", "C"]
    ans = []
    ans.append(a)
    a[2] = "D"
    ans.append(a)
    print(ans)


    I expected, ans as, [['A', 'B', 'C'], ['A', 'B', 'D']], but I got [['A', 'B', 'D'], ['A', 'B', 'D']]. Do you know why?

    Continue reading...

Compartilhe esta Página