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
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
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']
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']]