Search code examples
pythonrangemultiplication

Trying to get multiple values from a multiplication using a list with python


I am trying to include a list (with the use of a range) within a multiplication to get multiple values back:

say I have z = range(0, 26, 5)

how do I include this range into the formula:

t = (z * 4) + z

to then get back five different values? (i.e for every value of z)


Solution

  • You can use a for-loop over the range within square-brackets, which will get you a list. This is called a list comprehension.

    t = [(x * 4) + x for x in z]
    

    Isn't that the same as? :

    t = [(x * 5)  for x in z]