Undo python list comprehension

lst = [ x**2 for x in [x**2 for x in range(11)]]

# equivalent to:
squares = []
squares2 = []
for x in range(11):
    squares.append(x**2)

for item in squares:
    squares2.append(item**2)
    
print(squares2)
print(squares2==lst)

output:

[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]
True

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top