Menu

gitpiper

map_values python Code Snippet in 2024

dictionaryintermediate

Last Updated: 21 April 2024

Creates a dictionary with the same keys as the provided dictionary and values generated by running the provided function for each value.

  • Use dict.items() to iterate over the dictionary, assigning the values produced by fn to each key of a new dictionary.
def map_values(obj, fn): return dict((k, fn(v)) for k, v in obj.items())
users = { 'fred': { 'user': 'fred', 'age': 40 }, 'pebbles': { 'user': 'pebbles', 'age': 1 } } map_values(users, lambda u : u['age']) # {'fred': 40, 'pebbles': 1}

python snippet similar to map_values 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! ✌️