From 7f9c9b7062786e450358a3e3150688becb731110 Mon Sep 17 00:00:00 2001 From: AlanPCS Date: Wed, 21 Oct 2020 21:49:25 -0300 Subject: [PATCH 1/3] Add pluck --- snippets/pluck.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 snippets/pluck.md diff --git a/snippets/pluck.md b/snippets/pluck.md new file mode 100644 index 000000000..05d7121f1 --- /dev/null +++ b/snippets/pluck.md @@ -0,0 +1,25 @@ +--- +title: pluck +tags: list,dictionary,intemediary +--- + +Extracts a list of values from a dict given a key + +- Given an `array` of `dict`s, returns the list of values of the `key` passed from each dict record. +- When a dict does not have the `key` passed, returns `None` + +```py +def pluck(array, key): + return list(map(lambda entry: dict.get(entry, key), array)) +``` + +```py +simpsons = [ + { "name": "lisa", "age": 8 }, + { "name": "homer", "age": 36 }, + { "name": "marge", "age": 34 }, + { "name": "bart", "age": 10 }, +]; + +pluck(simpsons, "age") # [8, 36, 34, 10] +``` \ No newline at end of file From d740c3e9b9e91fbe5b1c2acb4252dd70ce3042f6 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Thu, 22 Oct 2020 10:09:21 +0300 Subject: [PATCH 2/3] Update pluck.md --- snippets/pluck.md | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/snippets/pluck.md b/snippets/pluck.md index 05d7121f1..749fef204 100644 --- a/snippets/pluck.md +++ b/snippets/pluck.md @@ -1,25 +1,23 @@ --- title: pluck -tags: list,dictionary,intemediary +tags: list,dictionary,beginner --- -Extracts a list of values from a dict given a key +Converts a list of dictionaries into a list of values corresponding to the specified `key`. -- Given an `array` of `dict`s, returns the list of values of the `key` passed from each dict record. -- When a dict does not have the `key` passed, returns `None` +- Use a list comprehension and `dict.get()` to get the value of `key` for each dictionary in `lst`. ```py -def pluck(array, key): - return list(map(lambda entry: dict.get(entry, key), array)) +def pluck(lst, key): + return [x.get(key) for x in lst] ``` ```py simpsons = [ - { "name": "lisa", "age": 8 }, - { "name": "homer", "age": 36 }, - { "name": "marge", "age": 34 }, - { "name": "bart", "age": 10 }, + { 'name': 'lisa', 'age': 8 }, + { 'name': 'homer', 'age': 36 }, + { 'name': 'marge', 'age': 34 }, + { 'name': 'bart', 'age': 10 } ]; - -pluck(simpsons, "age") # [8, 36, 34, 10] -``` \ No newline at end of file +pluck(simpsons, 'age') # [8, 36, 34, 10] +``` From 056cd658c6edf7e9841b7aa89042bc2a2a0365f6 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Thu, 22 Oct 2020 10:09:44 +0300 Subject: [PATCH 3/3] Update pluck.md --- snippets/pluck.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/pluck.md b/snippets/pluck.md index 749fef204..492023412 100644 --- a/snippets/pluck.md +++ b/snippets/pluck.md @@ -18,6 +18,6 @@ simpsons = [ { 'name': 'homer', 'age': 36 }, { 'name': 'marge', 'age': 34 }, { 'name': 'bart', 'age': 10 } -]; +] pluck(simpsons, 'age') # [8, 36, 34, 10] ```