Issue
The ideal output or results should be like below:
>>> uppercase_keys_from_kwargs(twn='Taiwan')
['TWN']
>>> uppercase_keys_from_kwargs(twn='Taiwan', jpn='Japan')
['TWN', 'JPN']
>>> uppercase_keys_from_kwargs(twn='Taiwan', jpn='Japan', ltu="Lithuania")
['TWN', 'JPN', 'LTU']
>>> uppercase_keys_from_kwargs(twn='Taiwan', jpn='Japan', ltu="Lithuania", svn='Slovenia')
['TWN', 'JPN', 'LTU', 'SVN']
My Code just able obtain the keys only:
def uppercase_keys_from_kwargs(**kwargs) -> list:
keys_only = kwargs.keys()
list_for_keys_only = list(keys_only)
return(list_for_keys_only)
My output:
>>> print(uppercase_keys_from_kwargs(twn='Taiwan'))
['twn']
>>> print(uppercase_keys_from_kwargs(twn='Taiwan', jpn='Japan'))
['twn', 'jpn']
>>> print(uppercase_keys_from_kwargs(twn='Taiwan', jpn='Japan', ltu="Lithuania"))
['twn', 'jpn', 'ltu']
>>> print(uppercase_keys_from_kwargs(twn='Taiwan', jpn='Japan', ltu="Lithuania", svn='Slovenia'))
['twn', 'jpn', 'ltu', 'svn']
Tried to add something like this:
# for k in kwargs.items():
# kwargs.update(k.upper())
but did not work. Appear to be an AttributeError
.
Solution
We can use map
with str.upper
on dict.keys
.
def uppercase_keys_from_kwargs(**kwargs):
return list(map(str.upper, kwargs.keys()))
Ouput:
uppercase_keys_from_kwargs(twn='Taiwan')
# ['TWN']
uppercase_keys_from_kwargs(
twn="Taiwan", jpn="Japan", ltu="Lithuania", svn="Slovenia"
)
# ['TWN', 'JPN', 'LTU', 'SVN']
Answered By - Ch3steR
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.