Skip to content

Home

Map list to dictionary

Maps the values of a list to a dictionary using a function, where the key-value pairs consist of the original value as the key and the result of the function as the value.

def map_dictionary(itr, fn):
  return dict(zip(itr, map(fn, itr)))

map_dictionary([1, 2, 3], lambda x: x * x) # { 1: 1, 2: 4, 3: 9 }

More like this

Start typing a keyphrase to see matching snippets.