Files
30-seconds-of-code/snippets/keys_only.md
2019-08-20 10:09:53 +03:00

21 lines
521 B
Markdown

---
title: keys_only
tags: object
---
Function which accepts a dictionary of key value pairs and returns a new flat list of only the keys.
Uses the .keys() method of "dict" objects. dict.keys() returns a view object that displays a list of all the keys. Then, list(dict.keys()) returns a list that stores all the keys of a dict.
```py
def keys_only(flat_dict):
return list(flat_dict.keys())
```
```py
ages = {
"Peter": 10,
"Isabel": 11,
"Anna": 9,
}
keys_only(ages) # ['Peter', 'Isabel', 'Anna']
```