Combine two lists Python | Example code

Use the + operator to Combine two lists in Python. There are many ways to join or concatenate, two or more lists in Python. Like, append method and extent method.

Example Combine two lists in Python

Simple example code.

Using the + operator

This is the easiest and most popular way to add 2 lists in Python.

list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

list3 = list1 + list2
print(list3)

Output:

Combine two lists Python

Append method+ for loop

Another way is appending all the items to another list using the for-loop and append method.

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

for x in list2:
    list1.append(x)

print(list1)

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

Using extend() method

Extend() function will add an item at the end of another list.

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


list1.extend(list2)
print(list1) 

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

How do I concatenate two lists in Python?

As of 3.9, these are the most popular stdlib methods for concatenating two (or more) lists in python.

concatenate two lists in Python

Source: stackoverflow.com

Do comment if you have any doubts and suggestions on this Python list code.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

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