How to combine two lists to make each element of the list have two values, one from each list?

Viewed 79

Okay so I have two lists. I am trying to upload them to a .json file but I first want to combine them. I would like to combine the two lists making each element of the merged list 2 parts. For example

list1 = [[4],[5],[6],[7]]
list2 = [["a"],["b"],["c"],["d"]]

Then once they are combined I would like them to look like this:

mergedList = [[4, "a"], [5, "b"], [6, "c"], [7, "d"]]

How would I go about doing this? If it makes it easier, all I am trying to do is save 3 data values to this .json PER item that I am searching for. Thanks!

2 Answers

You can do this with list comprehension,

In [18]: [i+j for i,j in zip(list1,list2)]
Out[18]: [[4, 'a'], [5, 'b'], [6, 'c'], [7, 'd']]

And just in case the elements in each list are not each inside its own list (which is a weird design tbh):

>>> list1 = ["a", "b", "c"]
>>> list2 = [1, 2, 3]
>>> [[i, j] for i, j in zip(list1,list2)]

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