[Python] Pythonic way to create a list of repeated different mutable objects [duplicate]

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    I want a list of five lists:

    c = [[],]*5


    but unfortunately:

    c[0].append(1)
    print(c)


    gives this unwanted result:

    [[1], [1], [1], [1], [1]]


    is there a way better than:

    [[] for _ in range(5)]
    enter code here


    Thanks

    Continue reading...

Compartilhe esta Página