Python Forum

Full Version: Multiplication between a list and a variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How do I code a multiplication between a list and a variable? The result would be all the values of the list multiplied by the variable. Is it possible? Example:

a=int(input("a=")) b=int(input("b=")) x=a-b l=[1,2,3,4,5] y=x*l print(y)
>>> a = 7 >>> b = 11 >>> x = a-b >>> l = [1,2,3,4,5] >>> print([n * x for n in l]) [-4, -8, -12, -16, -20]
(Oct-08-2019, 04:02 AM)Larz60+ Wrote: [ -> ]
>>> a = 7 >>> b = 11 >>> x = a-b >>> l = [1,2,3,4,5] >>> print([n * x for n in l]) [-4, -8, -12, -16, -20]
Great! Thank you so much!