Update remaining snippets
This commit is contained in:
@@ -1,24 +1,23 @@
|
||||
---
|
||||
title: values_only
|
||||
tags: object
|
||||
tags: object,list,beginner
|
||||
---
|
||||
Function which accepts a dictionary of key value pairs and returns a new flat list of only the values.
|
||||
|
||||
Uses the .items() function with a for loop on the dictionary to track both the key and value of the iteration and returns a new list by appending the values to it. Best used on 1 level-deep key:value pair dictionaries and not nested data-structures.
|
||||
Returns a flat list of all the values in a flat dictionary.
|
||||
|
||||
Use `dict.values()` to return the values in the given dictionary.
|
||||
Return a `list()` of the previous result.
|
||||
|
||||
```py
|
||||
def values_only(dict):
|
||||
lst = []
|
||||
for k, v in dict.items():
|
||||
lst.append(v)
|
||||
return lst
|
||||
return list(flat_dict.values())
|
||||
```
|
||||
|
||||
```py
|
||||
ages = {
|
||||
"Peter": 10,
|
||||
"Isabel": 11,
|
||||
"Anna": 9,
|
||||
"Peter": 10,
|
||||
"Isabel": 11,
|
||||
"Anna": 9,
|
||||
}
|
||||
values_only(ages) # [10, 11, 9]
|
||||
```
|
||||
Reference in New Issue
Block a user