Solved issue #53
This commit is contained in:
@ -2,14 +2,11 @@
|
|||||||
|
|
||||||
Function which accepts a dictionary of key value pairs and returns a new flat list of only the keys.
|
Function which accepts a dictionary of key value pairs and returns a new flat list of only the keys.
|
||||||
|
|
||||||
Uses the .items() function with a for loop on the dictionary to track both the key and value and returns a new list by appending the keys to it. Best used on 1 level-deep key:value pair dictionaries (a flat dictionary) and not nested data-structures which are also commonly used with dictionaries. (a flat dictionary resembles a json and a flat list an array for javascript people).
|
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.
|
||||||
|
|
||||||
``` python
|
``` python
|
||||||
def keys_only(flat_dict):
|
def keys_only(flat_dict):
|
||||||
lst = []
|
return list(flat_dict.keys())
|
||||||
for k, v in flat_dict.items():
|
|
||||||
lst.append(k)
|
|
||||||
return lst
|
|
||||||
```
|
```
|
||||||
|
|
||||||
``` python
|
``` python
|
||||||
|
|||||||
Reference in New Issue
Block a user