How to multiply each element in a list by a number in Python

4 Answers

0 votes
lst = [1, 2, 3, 4, 5, 6]

lst = [i * 2 for i in lst]

print(lst)



'''
run:
 
[2, 4, 6, 8, 10, 12]

'''

 



answered May 21, 2019 by avibootz
0 votes
lst = [1, 2, 3, 4, 5, 6]

new_lst = []
for i in lst:
    new_lst.append(i * 2)

print(new_lst)



'''
run:
 
[2, 4, 6, 8, 10, 12]

'''

 



answered May 21, 2019 by avibootz
0 votes
import pandas as pd

lst = [1, 2, 3, 4, 5, 6]

s = pd.Series(lst)

lst = (s * 2).tolist()

print(lst)



'''
run:
 
[2, 4, 6, 8, 10, 12]

'''

 



answered May 21, 2019 by avibootz
0 votes
lst = [1, 2, 3, 4, 5, 6]

lst[:] = [x * 2 for x in lst]

print(lst)



'''
run:
 
[2, 4, 6, 8, 10, 12]

'''

 



answered May 21, 2019 by avibootz
...