Chalarangelo / 30-seconds-of-python

Short Python code snippets for all your development needs
https://www.30secondsofcode.org/python/p/1
Creative Commons Attribution 4.0 International
8.83k stars 1.26k forks source link

[FEATURE] map_values() could be improved #101

Closed defterade closed 5 years ago

defterade commented 5 years ago

Category:

Description

Can be done in a single line.

Current Snippet Behavior

Current code:

def map_values(obj, fn):
    ret = {}
    for key in obj.keys():
        ret[key] = fn(obj[key])
    return ret

Possible Solution

Suggested replacement:

def map_values(obj, fn):
    return {key: fn(obj[key]) for key in obj}
Chalarangelo commented 5 years ago

Resolved in #102

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for any follow-up tasks.