add snippets keys_only + values_only + test bp
This commit is contained in:
23
snippets/keys_only.md
Normal file
23
snippets/keys_only.md
Normal file
@ -0,0 +1,23 @@
|
||||
### keys_only
|
||||
|
||||
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 and not nested data-structures.
|
||||
|
||||
``` python
|
||||
def keys_only(dict):
|
||||
lst = []
|
||||
for k, v in dict.items():
|
||||
lst.append(k)
|
||||
return lst
|
||||
```
|
||||
|
||||
``` python
|
||||
ages = {
|
||||
"Peter": 10,
|
||||
"Isabel": 11,
|
||||
"Anna": 9,
|
||||
}
|
||||
keys_only(ages) # result
|
||||
['Peter', 'Isabel', 'Anna']
|
||||
```
|
||||
23
snippets/values_only.md
Normal file
23
snippets/values_only.md
Normal file
@ -0,0 +1,23 @@
|
||||
### values_only
|
||||
|
||||
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.
|
||||
|
||||
``` python
|
||||
def values_only(dict):
|
||||
lst = []
|
||||
for k, v in dict.items():
|
||||
lst.append(v)
|
||||
return lst
|
||||
```
|
||||
|
||||
``` python
|
||||
ages = {
|
||||
"Peter": 10,
|
||||
"Isabel": 11,
|
||||
"Anna": 9,
|
||||
}
|
||||
values_only(ages) # result
|
||||
[10, 11, 9]
|
||||
```
|
||||
6
test/keys_only/Keys_only.test.py
Normal file
6
test/keys_only/Keys_only.test.py
Normal file
@ -0,0 +1,6 @@
|
||||
import types,functools
|
||||
from pytape import test
|
||||
from keys_only import keys_only
|
||||
def keys_only_test(t):
|
||||
t.true(isinstance(keys_only, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'keys_only is a function')
|
||||
test('Testing keys_only',keys_only_test)
|
||||
0
test/keys_only/keys_only.py
Normal file
0
test/keys_only/keys_only.py
Normal file
5
test/values_only/values_only.py
Normal file
5
test/values_only/values_only.py
Normal file
@ -0,0 +1,5 @@
|
||||
def values_only(dict):
|
||||
lst = []
|
||||
for k, v in dict.items():
|
||||
lst.append(v)
|
||||
return lst
|
||||
6
test/values_only/values_only.test.py
Normal file
6
test/values_only/values_only.test.py
Normal file
@ -0,0 +1,6 @@
|
||||
import types,functools
|
||||
from pytape import test
|
||||
from values_only import values_only
|
||||
def values_only_test(t):
|
||||
t.true(isinstance(values_only, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'values_only is a function')
|
||||
test('Testing values_only',values_only_test)
|
||||
Reference in New Issue
Block a user