diff --git a/snippets/keys_only.md b/snippets/keys_only.md new file mode 100644 index 000000000..7c2a3e478 --- /dev/null +++ b/snippets/keys_only.md @@ -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'] +``` \ No newline at end of file diff --git a/snippets/values_only.md b/snippets/values_only.md new file mode 100644 index 000000000..e326dd9a9 --- /dev/null +++ b/snippets/values_only.md @@ -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] +``` \ No newline at end of file diff --git a/test/keys_only/Keys_only.test.py b/test/keys_only/Keys_only.test.py new file mode 100644 index 000000000..6ba12d5cd --- /dev/null +++ b/test/keys_only/Keys_only.test.py @@ -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) \ No newline at end of file diff --git a/test/keys_only/keys_only.py b/test/keys_only/keys_only.py new file mode 100644 index 000000000..e69de29bb diff --git a/test/values_only/values_only.py b/test/values_only/values_only.py new file mode 100644 index 000000000..fb26aa46f --- /dev/null +++ b/test/values_only/values_only.py @@ -0,0 +1,5 @@ +def values_only(dict): + lst = [] + for k, v in dict.items(): + lst.append(v) + return lst \ No newline at end of file diff --git a/test/values_only/values_only.test.py b/test/values_only/values_only.test.py new file mode 100644 index 000000000..6a2f99517 --- /dev/null +++ b/test/values_only/values_only.test.py @@ -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) \ No newline at end of file