+ 1
Adding 1 to each number in a list
If I have a list, a=[1,2]. How would I create a while loop that adds 1 to each number in the list every time, so that the list would be [2,3],[3,4],....? Iâve tried a=[0] while True: a=[x+1 for x in a] But this only works when there is one number in the list. Is there a way to make this work with multiple numbers in the list?
2 Answers
+ 2
a = [1,2,3]
while True:
a = [x+1 for x in a]
print(a)
This is the right way import time and use sleep function to see its changing degit else it will be very very fast
+ 1
Is the output supposed to be like this?
[[1, 2], [2, 3], [3, 4], [4, 5] ... ]