0

Is there a quick and simple way to transform lists (assuming lists with same length) :

numbers = [1, 2, 3, 4]
letters = ['a', 'b', 'c', 'd']
fruits = ['orange', 'apple', 'banana', 'kiwi']

Into one with sublist with this form :

list = [ [1, 'a', 'orange'], 
         [2, 'b', 'apple'], 
         [3, 'c', 'banana'], 
         [4, 'd', 'kiwi'] ]

or eventually

list = [ {'number': 1, 'letter': 'a', 'fruit': 'oranges'}, 
         {'number': 2, 'letter': 'b', 'fruit': 'apple'}, 
         {'number': 3, 'letter': 'c', 'fruit': 'banana'}, 
         {'number': 4, 'letter': 'd', 'fruit': 'kiwi'} ]

Or must I do with multiple loops ?

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
JiDai
  • 136
  • 2
  • 3
  • 11
  • For numerical values this is easy with numpy (would be vstack((numbers, letters, fruits)).T ), However numpy yields no suitable solution for arbitrary lists afaik. – stewori Jun 30 '15 at 20:25
  • @stewori I hope you mean "no suitable solution _with `numpy`_". – TigerhawkT3 Jun 30 '15 at 20:26
  • Sure. I just clarified it. For numerical data the numpy-solution should be atually preferred over the general purpose answers below. – stewori Jun 30 '15 at 20:30
  • Ok, thanks. In my real code I have numerical values (mostly floats). – JiDai Jun 30 '15 at 20:38
  • Why my question is a duplicate ? The topic "How to extract column from a multi-dimentional array?" did not answer my problem... – JiDai Jun 30 '15 at 20:43

3 Answers3

2

You can use the built-in zip function. For first one, just zip them all together and then use map function to tranform inner tuples to list and then make them lists.

Example -

numbers = [1, 2, 3, 4]
letters = ['a', 'b', 'c', 'd']
fruits = ['orange', 'apple', 'banana', 'kiwi']
>>> list(map(list, zip(numbers,letters,fruits)))
[[1, 'a', 'orange'], [2, 'b', 'apple'], [3, 'c', 'banana'], [4, 'd', 'kiwi']]

Please note for Python 2.x , you do not need the list(...) function that is surrounding the map(..) function, since it returns list , but in Python 3.x map(..) function returns a generator, so if you need list , you need to convert it to list.

For the second one , you can use list comprehension along with zip function, as -

>>> list({'number':x,'letter':y,'fruit':z} for x,y,z in zip(numbers,letters,fruits))
[{'number': 1, 'letter': 'a', 'fruit': 'orange'}, {'number': 2, 'letter': 'b', 'fruit': 'apple'}, {'number': 3, 'letter': 'c', 'fruit': 'banana'}, {'number': 4, 'letter': 'd', 'fruit': 'kiwi'}]
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
1
>>> dicts = [dict(zip(('fruit','letter','number'),vals)) for vals in zip(fruits,letters,numbers)]
>>> dicts
[{'fruit': 'orange', 'number': 1, 'letter': 'a'}, {'fruit': 'apple', 'number': 2, 'letter': 'b'}, {'fruit': 'banana', 'number': 3, 'letter': 'c'}, {'fruit': 'kiwi', 'number': 4, 'letter': 'd'}]
Blair
  • 6,623
  • 1
  • 36
  • 42
0

Here is a nice one for the first:

>>> a = [1, 2, 3, 4]
>>> b = ['a', 'b', 'c', 'd']
>>> c = ['orange', 'apple', 'banana', 'kiwi']
>>> [[x, y, z] for x, y, z in zip(a, b, c)]
[[1, 'a', 'orange'], [2, 'b', 'apple'], [3, 'c', 'banana'], [4, 'd', 'kiwi']]