0

I have two list as follows (both list contains super long data):

list_1 = ["apple", "orange", "banana", ......]
list_2 = [[1,0,0,0,0], [0.5,0,0.5,0,0], [0,0,1,0,0], ......]

I want to have an outcome of :

list = {"apple" : [1,0,0,0,0], "orange" : [0.5,0,0.5,0,0], "banana" : [0,0,1,0,0], .......]

What I want to do is like putting list_1[0] to list_2[0], list_1[1] to list_2[1] and so on.

Is there a way to do automatically pair the data up no matter how long the datasets are ?

Thanks

Leigh
  • 188
  • 2
  • 13

1 Answers1

-1

You can use zip:

list = {}
for x, y in zip(list_1, list_2):
    list[x] = y
Thomas Schillaci
  • 2,348
  • 1
  • 7
  • 19
  • 1
    You can save on the loop and simply use the `dict` constructor: `dict(zip(list_1, list_2))` – Tomerikoo Feb 26 '20 at 11:44
  • For someone who doesn't know how to create a dict, it may be easier to have a comprehensive example rather than a quick one. But yes, one-liner are prettier if you know what you're doing. – Thomas Schillaci Feb 26 '20 at 11:54
  • 2
    It's not about being a one-liner or not. It's being simple, readable code. And for someone who doesn't know how to create a dict, it might be very useful to learn using the `dict` constructor. Just pointing out a neat little alternative. BTW, you call your dict `list` which is first of all confusing (because it is not a list) and second shadows the built-in `list`... – Tomerikoo Feb 26 '20 at 11:58