[Python] Built-in list.append vs list.extend selection? [closed]

Discussão em 'Python' iniciado por Stack, Julho 16, 2025.

  1. Stack

    Stack Membro Participativo

    When working with lists, there are two member functions of interest:

    • list.append(obj) - Appends a single object to a list. If obj happens to be a list, it will become a list-within-a-list
    • list.extend(list_2) - Adds every item in list_2 to the list

    Basically, what I want to do is this:

    list = []
    try:
    iter(something)
    list.extend(something)
    except TypeError:
    list.append(something)


    but in some simple one-line fashion. Does such syntax exist?

    Continue reading...

Compartilhe esta Página