-1

I am trying to get the value of 2 python lists.

list1 = ['a','b','c']
list1 = ['1','2','3']

the output

['a','1']
['b','2']
['c','3']

I am still beginner with python

Any help will be apperciate

  • 4
    Looking into the [zip](https://docs.python.org/3.4/library/functions.html#zip) function would be a start. – Andy G Dec 14 '17 at 11:48
  • `[[x, y] for x, y in zip(list1, list2)]` – RoadRunner Dec 14 '17 at 11:49
  • try this [list(a) for a in zip(list1, list2)] – sujit tiwari Dec 14 '17 at 11:52
  • 1
    I think @AndyG 's comment is a good pointer for the OP to start formulating a solution. I don't understand why answers are being posted. Some time should be allotted for the OP to look into the concept of zip. If the OP is unable to come up with a solution, he / she will edit the question / comment asking for more help. – Ganesh Tata Dec 14 '17 at 11:59

2 Answers2

1

You can use the zip builtin to generate tuples containing an element for each corresponding position from each list

list1 = ['a','b','c']
list2 = ['1','2','3']
for tuple in (zip(list1, list2)):
    # Cast each tuple to a list and print it
    print(list(tuple))
>>['a', '1']
>>['b', '2']
>>['c', '3']
BoboDarph
  • 2,751
  • 1
  • 10
  • 15
0

Use map for generic usage:

map(lambda *x: list(x), list1, list2)
[['a', '1'], ['b', '2'], ['c', '3']]

or zip within a list comprehension:

[list(v) for v in zip(list1, list2)]
[['a', '1'], ['b', '2'], ['c', '3']]

both of them can be easily expanded for more lists:

list3 = ["9", "8", "7"]

map(lambda *x: list(x), list1, list2, list3)
[['a', '1', '9'], ['b', '2', '8'], ['c', '3', '7']]

[list(v) for v in zip(list1, list2, list3)]
[['a', '1', '9'], ['b', '2', '8'], ['c', '3', '7']]
Netwave
  • 40,134
  • 6
  • 50
  • 93