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

update map_dictionary #484

Closed zhangfelix closed 2 years ago

zhangfelix commented 2 years ago

Using dictionary comprehension instead of map and zip can simplify the snippet, while enhancing the readability of the code, making it easier to understand.

Before:

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

After:

def map_dictionary(itr, fn):
  return {x:fn(x) for x in itr}