6

I want to merge the 2 lists:

a = [1,2,3,4,"a"]
b = [1,2,3,4,"b"]

to make:

[[1,1], [2,2], [3,3], [4,4], ["a","b"]]

What would be the best way of doing this?

Also if possible I would like to append further lists as well like so:

c = [5,6,7,8,"c"]

to get

[[1,1,5], [2,2,6], [3,3,7], [4,4,8], [5,5,9], ["a","b","c"]

You can assume the lists are the same length.

Dave
  • 390
  • 1
  • 6
  • 16
  • yes i realise it maybe a duplicate question but i did not find anything by searching – Dave Apr 27 '14 at 18:30
  • 2
    He's not asking for a list of tuples, but a list of lists – jshanley Apr 27 '14 at 18:30
  • 1
    why people frenetically downvote such a question ? – Stéphane Laurent Apr 27 '14 at 18:35
  • I could probably use tuples instead as the data is immutable after this point. final goal is to get json output from 500+ lists with 100+ variables in each, with a separate dict prepended to each list inside each list. – Dave Apr 27 '14 at 18:35
  • 1
    @StéphaneLaurent https://www.google.co.in/search?q=merge+two+list+into+list+of+lists+python First hit, and converting a tuple to list is very trivial. – Ashwini Chaudhary Apr 27 '14 at 18:37
  • 1
    @StéphaneLaurent You might even argue that the list element is different from the referenced question. – devnull Apr 27 '14 at 18:41
  • okay, i realise it is a bit of a newbie question now. thanks everybody for your time. – Dave Apr 27 '14 at 18:42
  • @Aशwiniचhaudhary Ok, but the score attained -3 when I wrote my comment (that's what I call "frenetic"), and none explanation in the comments. Personnally I never downvote a question already having a negative score, except if it is really a piss-take. This is just my personal opinion. – Stéphane Laurent Apr 27 '14 at 21:11
  • @devnull I don't understand what you say (my English). I have re-read the other topic, for me this is not the same question. *"Converting a tuple to list is very trivial"* is true for someone who has a minimal knowledge in Python. Is it forbidden to be a beginner ? – Stéphane Laurent Apr 27 '14 at 21:16

2 Answers2

10

You can use zip and a list comprehension:

>>> a = [1, 2, 3, 4, "a"]
>>> b = [1, 2, 3, 4, "b"]
>>> new_lst = [list(x) for x in zip(a, b)]
>>> new_lst
[[1, 1], [2, 2], [3, 3], [4, 4], ['a', 'b']]
>>>

Edit:

Regarding your updated question, if you later want to add in another list, you can use this:

>>> c = [5, 6, 7, 8, "c"]
>>> [x + [y] for x,y in zip(new_lst, c)]
[[1, 1, 5], [2, 2, 6], [3, 3, 7], [4, 4, 8], ['a', 'b', 'c']]
>>>

Of course, if you have all three lists to begin with, all you need to do is give another argument to zip:

>>> a = [1, 2, 3, 4, "a"]
>>> b = [1, 2, 3, 4, "b"]
>>> c = [5, 6, 7, 8, "c"]
>>> new_lst = [list(x) for x in zip(a, b, c)]
>>> new_lst
[[1, 1, 5], [2, 2, 6], [3, 3, 7], [4, 4, 8], ['a', 'b', 'c']]
>>>
1

Variation of accepted solution

As it is explicitly declaring items for sublist, it seems to me a bit more readable:

>>> a = [1,2,3,4,"a"]
>>> b = [1,2,3,4,"b"]
>>> c = [5,6,7,8,"c"]
>>> [[ai, bi, ci] for ai, bi, ci in zip(a, b, c)]
[[1, 1, 5], [2, 2, 6], [3, 3, 7], [4, 4, 8], ['a', 'b', 'c']]

Shortes variant using map

>>> map(list, zip(a, b, c))
[[1, 1, 5], [2, 2, 6], [3, 3, 7], [4, 4, 8], ['a', 'b', 'c']]
Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98