[Python] Remove element from list and print new list in the same statement

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am using the .remove() function to remove an element from a list and then I want to use directly this new list into another function e.g. the print() function. When I am doing this then I get the following:

    print([1, 2, 3])
    # [1, 2, 3]

    print([1, 2, 3].remove(2))
    # None


    So if I want to use directly another function (e.g. print()) after an Inplace function such as .remove()then paradoxically I can only do this like this:

    print([1, 2, 3])
    # [1, 2, 3]

    x = [1, 2, 3]
    x.remove(2)
    print(x)
    # [1, 3]


    Is there any better way to do this instead of writing this additional source code?

    Apparently the same problem applies for all Inplace functions as attributes of other functions I guess.

    Continue reading...

Compartilhe esta Página