Update pluck.md
This commit is contained in:
committed by
GitHub
parent
7f9c9b7062
commit
d740c3e9b9
@ -1,25 +1,23 @@
|
|||||||
---
|
---
|
||||||
title: pluck
|
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.
|
- Use a list comprehension and `dict.get()` to get the value of `key` for each dictionary in `lst`.
|
||||||
- When a dict does not have the `key` passed, returns `None`
|
|
||||||
|
|
||||||
```py
|
```py
|
||||||
def pluck(array, key):
|
def pluck(lst, key):
|
||||||
return list(map(lambda entry: dict.get(entry, key), array))
|
return [x.get(key) for x in lst]
|
||||||
```
|
```
|
||||||
|
|
||||||
```py
|
```py
|
||||||
simpsons = [
|
simpsons = [
|
||||||
{ "name": "lisa", "age": 8 },
|
{ 'name': 'lisa', 'age': 8 },
|
||||||
{ "name": "homer", "age": 36 },
|
{ 'name': 'homer', 'age': 36 },
|
||||||
{ "name": "marge", "age": 34 },
|
{ 'name': 'marge', 'age': 34 },
|
||||||
{ "name": "bart", "age": 10 },
|
{ 'name': 'bart', 'age': 10 }
|
||||||
];
|
];
|
||||||
|
pluck(simpsons, 'age') # [8, 36, 34, 10]
|
||||||
pluck(simpsons, "age") # [8, 36, 34, 10]
|
```
|
||||||
```
|
|
||||||
|
|||||||
Reference in New Issue
Block a user