How to append multiple items in one line in Python

I have:

    count = 0
    i = 0
    while count < len(mylist):
        if mylist[i + 1] == mylist[i + 13] and mylist[i + 2] == mylist[i + 14]:
            print mylist[i + 1], mylist[i + 2]
        newlist.append(mylist[i + 1])
        newlist.append(mylist[i + 2])
        newlist.append(mylist[i + 7])
        newlist.append(mylist[i + 8])
        newlist.append(mylist[i + 9])
        newlist.append(mylist[i + 10])
        newlist.append(mylist[i + 13])
        newlist.append(mylist[i + 14])
        newlist.append(mylist[i + 19])
        newlist.append(mylist[i + 20])
        newlist.append(mylist[i + 21])
        newlist.append(mylist[i + 22])
        count = count + 1
        i = i + 12

I wanted to make the newlist.append() statements into a few statements.

No. The method for appending an entire sequence is list.extend().

    >>> L = [1, 2]
    >>> L.extend((3, 4, 5))
    >>> L
    [1, 2, 3, 4, 5]

From: stackoverflow.com/q/16621498

Back to homepage or read more recommendations: