+ 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?

31st Jul 2020, 4:59 AM
‱—‱ ‱ ‱-‱ ‱‱‱ —- -‱
‱—‱ ‱ ‱-‱ ‱‱‱ —- -‱ - avatar
2 RĂ©ponses
+ 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
31st Jul 2020, 5:04 AM
Ayush Kumar
Ayush Kumar - avatar
+ 1
Is the output supposed to be like this? [[1, 2], [2, 3], [3, 4], [4, 5] ... ]
31st Jul 2020, 8:13 AM
Ipang