Files
30-seconds-of-code/snippets/map_object.md
Angelos Chalaris 3de4296dc8 Add map_object
2020-03-16 19:51:03 +02:00

499 B

title, tags
title tags
map_object list,intermediate

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

Use a for loop to iterate over the list's values, assigning the values produced by fn to each key of the dictionary.

def map_object(itr, fn):
  ret = {}
  for x in itr:
    ret[x] = fn(x)
  return ret
map_object([1,2,3], lambda x: x * x) # { 1: 1, 2: 4, 3: 9 }