0

I have lists like..

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

i need output like

l3=[[1,2,3],[7,3,4]]
2
  • Why do you want to do it in pandas, you can do it in just normal python. Commented Apr 29, 2016 at 9:52
  • sorry i dont need to do pandas.....sorry... Commented Apr 29, 2016 at 9:53

5 Answers 5

4

You can simply do something like this:

>>> l1=[1,2,3]
>>> l2=[7,3,4]
>>> [l1, l2]
[[1, 2, 3], [7, 3, 4]]
Sign up to request clarification or add additional context in comments.

Comments

2

You can just do this

l3 = [l1,l2]

3 Comments

and i have another one doubt
is it possible to converts particular columns to convert lists..?
in my dataframe there are 5 columns...i wants convert particular 3 columns into lists
2

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]

Comments

1

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]]

1 Comment

That is exactly the output that I'm getting. Are you sure you included the square brackets?
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.