+ 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

3rd Sep 2017, 6:08 AM
ARC
ARC - avatar
3 odpowiedzi
+ 8
You can use map lst=map(lambda x:x*2,list) print(lst)
3rd Sep 2017, 6:11 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
Good you learnt. 👍
17th Sep 2017, 2:28 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 1
Thanks to the above answer by Blade, I was able to come up with the following. https://code.sololearn.com/cECkcHaYM9Ro/?ref=app
17th Sep 2017, 2:16 AM
ARC
ARC - avatar