2

I need to combine 3 lists into one list so that I can insert it smoothly into sqlite table.

list1= [[a1,b1,c1],[a2,b2,c2]]
list2= [[d1,e1,f1],[d2,e2,f2]]

Output should look like:

combined_list = [[a1,b1,c1,d1,e1,f1],[a2,b2,c2,d2,e2,f2]]

I tried sum list1 + list2 but both didn't work as this output.

1 Answer 1

2

You can try this:

from operator import add

a=[[1, 2, 3], [4, 5, 6]]
b=[['a', 'b', 'c'], ['d', 'e', 'f']]
print a + b
print map(add, a, b)

Output:

[[1, 2, 3], [4, 5, 6], ['a', 'b', 'c'], ['d', 'e', 'f']]
[[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f']]

Edit: To add more than two arrays:

u=[[]]*lists[0].__len__()
for x in lists: 
   u=map(add, u, x)
Sign up to request clarification or add additional context in comments.

3 Comments

If there are more lists to combine? I've to combine 4 lists into one.
You can repeat it as many times you need. from operator import add u=map(add, a,b); u=map(add, u,c); u=map(add, u,d); print u; Or try to automate it: u=[[]]*lists[0].__len__(); for x in lists: u=map(add,u,x);
Worked perfectly! Thank you

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.