How do I multiply each element in a list by a number?

后端 未结 8 1211
囚心锁ツ
囚心锁ツ 2020-11-27 04:29

I have a list:

my_list = [1, 2, 3, 4, 5]

How can I multiply each element in my_list by 5? The output should be:



        
8条回答
  •  孤街浪徒
    2020-11-27 04:46

    With map (not as good, but another approach to the problem):

    list(map(lambda x: x*5,[5, 10, 15, 20, 25]))
    

    also, if you happen to be using numpy or numpy arrays, you could use this:

    import numpy as np
    list(np.array(x) * 5)
    

提交回复
热议问题