Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #57 from mattveraldi/fix-keys_only
Browse files Browse the repository at this point in the history
Solved issue #53
  • Loading branch information
Chalarangelo committed Jul 18, 2019
2 parents 77d8ab9 + 1f4aeb5 commit be0d1cb
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 10 deletions.
2 changes: 1 addition & 1 deletion contributor_database
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ insertion_sort:[Meet Zaveri](@meetzaveri),[Rohit Tanwar](@kriadmin)
difference_by:[Rohit Tanwar](@kriadmin)
bubble_sort: [Shobhit Sachan](@sachans)
has_duplicates: [Rob-Rychs](@Rob-Rychs)
keys_only: [Rob-Rychs](@Rob-Rychs)
keys_only: [Rob-Rychs](@Rob-Rychs),[Matteo Veraldi](@mattveraldi)
values_only: [Rob-Rychs](@Rob-Rychs)
all_unique: [Rob-Rychs](@Rob-Rychs)
7 changes: 2 additions & 5 deletions snippets/keys_only.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

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
def keys_only(flat_dict):
lst = []
for k, v in flat_dict.items():
lst.append(k)
return lst
return list(flat_dict.keys())
```

``` python
Expand Down
5 changes: 1 addition & 4 deletions test/keys_only/keys_only.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
def keys_only(flat_dict):
lst = []
for k, v in flat_dict.items():
lst.append(k)
return lst
return list(flat_dict.keys())

0 comments on commit be0d1cb

Please sign in to comment.