Yasir Azeem
Yasir Azeem

Reputation: 329

python: combine lists of lists for SQLITE table

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.

Upvotes: 2

Views: 38

Answers (1)

Jaroslaw Matlak
Jaroslaw Matlak

Reputation: 574

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)

Upvotes: 2

Related Questions