Files
30-seconds-of-code/snippets/to-dictionary.md
Angelos Chalaris f6a215e9e3 Kebab file names
2023-04-27 22:00:06 +03:00

718 B

title, tags, excerpt, cover, firstSeen, lastUpdated
title tags excerpt cover firstSeen lastUpdated
Lists to dictionary list,dictionary Combines two lists into a dictionary, using the first one as the keys and the second one as the values. purple-sunset 2020-04-13T19:30:29+03:00 2020-11-02T19:28:35+02:00

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 dict() to combine the values of the two lists into a dictionary.
def to_dictionary(keys, values):
  return dict(zip(keys, values))
to_dictionary(['a', 'b'], [1, 2]) # { a: 1, b: 2 }