Python append list to another list | Example code

  • by

Use list append() or extend() the method to append the list to another list in Python. If you are appending one list to another list then all the elements of the second list will be added to the first list.

Note: Use itertools.chain() to combine multiple lists.

Python example Append list to another list

Simple example code.

Using list.extend()

This method will append the list parameter after the last element of the main list.

list1 = [1, 2, 3]
list2 = ["A", "B", "C"]

list1.extend(list2)
print(list1)

Output:

Python append list to another list

Using list.append()

list1 = [1, 2, 3]
list2 = ["A", "B", "C"]

list1.append(list2)
print(list1)

Output: [1, 2, 3, [‘A’, ‘B’, ‘C’]]

You can also use a For Loop to iterate over the elements of the second list and append each of these elements to the first list using the list.append() function.

list1 = [1, 2, 3]
list2 = ["A", "B", "C"]

for item in list2:
    list1.append(item)

print(list1)

Use the Concatenation + Operator to Append

The output will be a single combined list in order of the operands inputted in the code.

list1 = [1, 2, 3]
list2 = ["A", "B", "C"]


print(list1 + list2)

Output: [1, 2, 3, ‘A’, ‘B’, ‘C’]

Do comment if you have any doubts or suggestions on this Python List tutorial.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.