[Python] Python append() vs. += operator on lists, why do these give different results?

Discussão em 'Python' iniciado por Stack, Agosto 2, 2025.

  1. Stack

    Stack Membro Participativo

    Why do these two operations give different results?

    >>> c = [1, 2, 3]
    >>> c
    [1, 2, 3]
    >>> c += c
    >>> c
    [1, 2, 3, 1, 2, 3]
    >>> c = [1, 2, 3]
    >>> c.append(c)
    >>> c
    [1, 2, 3, [...]]
    >>>


    In the last case there's actually infinite recursion. c[-1] and c are the same. Why is it different with the + operation?

    Continue reading...

Compartilhe esta Página