+ 1
Python Lists. Computing each element in a list, individually.
What do I do to have a mathematical operation on each element in a List? The following operation: print(list * 2) displays the list twice one after another. I want to know, how I can display a result by multiplying each element of the list( which are a integers) by 2. Eg: list = [1,2,4] print(list * 2) output>>> 1,2,4,1,2,4 desired output>>> 2,4,8
3 Respostas
+ 8
You can use map
lst=map(lambda x:x*2,list)
print(lst)
+ 2
Good you learnt. 👍
+ 1
Thanks to the above answer by Blade, I was able to come up with the following.
https://code.sololearn.com/cECkcHaYM9Ro/?ref=app