From 89941e0c559bc5d442da5b49d234e035632a46ec Mon Sep 17 00:00:00 2001 From: Azan Bin Zahid <47540683+azanbinzahid@users.noreply.github.com> Date: Fri, 2 Oct 2020 01:05:52 +0500 Subject: [PATCH] alternate one liner implementation --- snippets/map_dictionary.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/snippets/map_dictionary.md b/snippets/map_dictionary.md index 0f9633188..f64de87e2 100644 --- a/snippets/map_dictionary.md +++ b/snippets/map_dictionary.md @@ -5,14 +5,13 @@ tags: list,intermediate 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 a `for` loop to iterate over the list's values, assigning the values produced by `fn` to each key of the dictionary. +- 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 a key value pair. ```py def map_dictionary(itr, fn): - ret = {} - for x in itr: - ret[x] = fn(x) - return ret + return dict(zip(itr, map(fn, itr))) ``` ```py