Add pluck

This commit is contained in:
AlanPCS
2020-10-21 21:49:25 -03:00
parent a6e58007bd
commit 7f9c9b7062

25
snippets/pluck.md Normal file
View File

@ -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]
```