Update remaining snippets

This commit is contained in:
Angelos Chalaris
2019-08-20 11:18:55 +03:00
parent a01dae2945
commit 62581cea2f
6 changed files with 54 additions and 48 deletions

View File

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