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

Issue

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]

Answered By – nessuno

Answer Checked By – Candace Johnson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.