-
-
Save IvanFrecia/7ab38759a61c4db8d9eb0d54736e85e8 to your computer and use it in GitHub Desktop.
# 1) Using map, create a list assigned to the variable greeting_doubled that doubles each element in the | |
# list lst. | |
lst = [["hi", "bye"], "hello", "goodbye", [9, 2], 4] | |
greeting_doubled = map((lambda a_list: a_list * 2), lst) | |
print(greeting_doubled) | |
# Output: | |
# [['hi', 'bye', 'hi', 'bye'], 'hellohello', 'goodbyegoodbye', [9, 2, 9, 2], 8] | |
# 2. Below, we have provided a list of strings called abbrevs. Use map to produce a new list called | |
# abbrevs_upper that contains all the same strings in upper case. | |
abbrevs = ["usa", "esp", "chn", "jpn", "mex", "can", "rus", "rsa", "jam"] | |
abbrevs_upper = map(lambda string: string.upper(), abbrevs) | |
print(abbrevs_upper) | |
# Output: | |
# ['USA', 'ESP', 'CHN', 'JPN', 'MEX', 'CAN', 'RUS', 'RSA', 'JAM'] |
@miriamputter you need to cast to list because it returns an iterable.
@miriamputter you need to cast to list because it returns an iterable.
How do I do that? Can you write the complete code?
For the first one:
greeting_doubled=list(map((lambda value: 2*value), lst))
print(greeting_doubled)
For the second one:
abbrevs_upper=list(map(lambda x: x.upper(), abbrevs))
print(abbrevs_upper)
For the first one:
greeting_doubled=list(map((lambda value: 2*value), lst)) print(greeting_doubled)
For the second one: abbrevs_upper=list(map(lambda x: x.upper(), abbrevs)) print(abbrevs_upper)
Thank you!
lst = [["hi", "bye"], "hello", "goodbye", [9, 2], 4]
greeting_doubled = list(map((lambda value: 2*value), lst))
print(greeting_doubled)
you can use this instead
and for the second one
abbrevs_upper = list(map((lambda str: str.upper()), abbrevs))
print(abbrevs_upper)
Thanks! I forgot in the latest version of python you have to cast to list in these cases...
Unfortunately, this code is not working to 100% and is showing only 80% of the result.
Why?