close

How do I merge lists in python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How do I merge lists in python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How do I merge lists in python?

  1. How do I merge lists in python?

    Building on the same idea, if you want to join multiple lists or a list of lists to a single list, you can still use “+” but inside a reduce method

  2. merge lists in python

    Building on the same idea, if you want to join multiple lists or a list of lists to a single list, you can still use “+” but inside a reduce method

Method 1


+
 operator can be used to merge two lists.

data1 = [1, 2, 3]
data2 = [4, 5, 6]

data = data1 + data2

print(data)

# output : [1, 2, 3, 4, 5, 6]

Lists can be merged like this in python.

Building on the same idea, if you want to join multiple lists or a list of lists to a single list, you can still use “+” but inside a reduce method like this,

from functools import reduce 

l1 = [1, 2, 3]
l2 = [4, 5, 6]
l3 = [7, 8, 9]
l4 = [10, 11, 12]

l = [l1, l2, l3, l4]

data = reduce(lambda a, b: a+b, l)
print(data)

# output : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Method 2

By using the + operator, like this:

>>> [1, 2] + [3, 4]
[1, 2, 3, 4]

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read