Menu

gitpiper

map_dictionary python Code Snippet in 2024

listdictionaryintermediate

Last Updated: 23 April 2024

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.

  • Use map() to apply fn to each value of the list.
  • Use zip() to pair original values to the values produced by fn.
  • Use dict() to return an appropriate dictionary.
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 }

python snippet similar to map_dictionary For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️