Sai Rajesh
Sai Rajesh

Reputation: 1982

how to insert list into list using python?

I have lists like..

l1=[1,2,3]
l2=[7,3,4]

i need output like

l3=[[1,2,3],[7,3,4]]

Upvotes: 0

Views: 1270

Answers (5)

exo
exo

Reputation: 41

For: l1 = [1, 2, 3] and l2 = [7, 3, 4] and l3 = [] you can:

append elements

l3.append(l1) and l3.append(l2)

create directly

l3 = [l1, l2]

add

l3 = [l1] + [l2]

Upvotes: 2

Strik3r
Strik3r

Reputation: 1057

In this case you can use

l3=[l1,l2]

If you want to insert l2 into l1 at some particular index you can make use of .insert function of lists

list.insert(index,object)
l1.insert(2,l2)

Hope it helps.

Upvotes: 1

Noe Fernandez
Noe Fernandez

Reputation: 114

You can just do this

l3 = [l1,l2]

Upvotes: 2

Nafiul Islam
Nafiul Islam

Reputation: 82440

You can simply do something like this:

>>> l1=[1,2,3]
>>> l2=[7,3,4]
>>> [l1, l2]
[[1, 2, 3], [7, 3, 4]]

Upvotes: 4

Pep_8_Guardiola
Pep_8_Guardiola

Reputation: 5252

Based on your update not requiring pandas, you can simply do this:

l1=[1,2,3]
l2=[7,3,4]

l3 = [l1] + [l2]

Output:

[[1, 2, 3], [7, 3, 4]]

Upvotes: 1

Related Questions