To change the list as we loop across it, use indexes so we can assign an updated value to each position.
L = [1, 2, 3, 4, 5] for i in range(len(L)): # Add one to each item in L L[i] += 1 # Or L[i] = L[i] + 1 print( L )
Using a while loop
L = [1, 2, 3, 4, 5] i = 0 # from www. j a v a 2 s .c o m while i < len(L): L[i] += 1 i += 1 print( L )
Using list comprehension expression of the form:
L = [1, 2, 3, 4, 5] A = [x + 1 for x in L] print(A)# from w ww . j ava 2s. c om