Files
30-seconds-of-code/snippets/to_dictionary.md
Isabelle Viktoria Maciohsek 2e9de879b6 Add to_dictionary
2020-04-13 19:30:50 +03:00

557 B

title, tags
title tags
to_dictionary list,dictionary,intermediate

Combines two lists into a dictionary, where the elements of the first one serve as the keys and the elements of the second one serve as the values. The values of the first list need to be unique and hashable.

Use zip() in combination with a list comprehension to combine the values of the two lists, based on their positions.

def to_dictionary(keys, values):
  return {key:value for key, value in zip(keys, values)}
to_dictionary(['a', 'b'], [1, 2]) # { a: 1, b: 2 }