1

Python using nested list comprehensions to create a accelerated increase

I want a list made out of numbers increasing with a multiplier which increases in every step. The basic nested for loop is easy.

numbers = []
i = 1
for x in range(50):
    numbers.append(x*i)
    i += 1

But when I try to do it with list comprehension, it doesn't work like I want it to be.

numbers = [x * i for x in range(50) for i in range(50)]

I know I did something wrong with the list comprehension but I don't know how to fix it.

Submitted July 23rd 2021 by Admin

Answers
0

This should behave the same way your for loop does.

numbers = [x * (i+1) for x, i in zip(range(50), range(50))]

or

numbers = [x * i for x, i in zip(range(50), range(1, 51))]

Admin | 1 month ago



Relevant Questions