Skip to content

Python merge two lists

  • by

Use the + operator to merge two lists in Python. There are several ways to merge, or concatenate, two or more lists in Python. But this one is the easiest way.

joinedlist = listone + listtwo

Python merges two lists

Simple example code.

list_one = [1, 2, 3]
list_two = [4, 5, 6]

res = list_one + list_two
print(res)

Output:

Python merges two lists

Or you can merge two lists by appending all the items from list2 into list1, one by one:

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

for x in list2:
  list1.append(x)

print(list1) 

What is the fastest way to merge two lists in Python?

Answer: You can just use concatenation:

list_1 = [1,2,3,4]
list_2 = [5,6,7,8]
list = list_1 + list_2

If you don’t need to keep list_1 around, you can just modify it using extend() method

list_1.extend(list_2)

Use += or .extend() if you want to merge in place. They are significantly faster.

Do comment if you have any doubts or suggestions on this Python list merge topic.

Note: IDE: PyCharm 2021.3.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

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading