-2

is there a way to define a function that takes two lists and makes the elements in the first list the key and the elements in the second list the values of a dictionary?

names = ['ted','west','tom','jerry','fred'] and another list ages = [10,23,13,18,12] how can i define a function that takes the two lists and returns a dictionary with the names as the key and the scores as the value?

i know that i can turn each list into a dictionary and then append it but I'm not sure how i can place the colons.

Vedang Mehta
  • 2,214
  • 9
  • 22
james2121
  • 13
  • 3
  • The answer that was marked as duplicate will work for you. But you can also do something like this: `{k:v for k,v in zip(names, ages)}`. This would be how to place those "colons", but you can also call `dict` which works just as well. – Chrispresso May 24 '16 at 19:14

1 Answers1

0

zip them, followed by a dict comprehension

>>> ages = [10,23,13,18,12]

>>> names = ['ted','west','tom','jerry','fred']

>>> {x[0]:x[1] for x in zip(names, ages)}
{'ted': 10, 'tom': 13, 'fred': 12, 'jerry': 18, 'west': 23}
Ayush
  • 3,695
  • 1
  • 32
  • 42