-1

Suppose i have 3 lists

[1,2,3]
['one','two','three']
['first','second','third']

I need to combine this into a single list like

[[1,'one','first'],[2,'two','second','third'],[3,'three','third']]

How do we do this?Using list comprehension?Is there any other best method?

akhilviswam
  • 95
  • 1
  • 11

1 Answers1

3

Use zip

>>>list(zip([1,2,3],['one','two','three'],['first','second','third']))
[(1, 'one', 'first'), (2, 'two', 'second'), (3, 'three', 'third')]

or as list of lists

>>>list(map(list, zip([1,2,3],['one','two','three'],['first','second','third'])))
[[1, 'one', 'first'], [2, 'two', 'second'], [3, 'three', 'third']]

Note: The outermost list call exists only to provide immediate evaluation of the map/zip functions and are not required if you will iterate over them later.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61