New To Learning Python; Why Is The Print For This For-loop Different When I Use A List Comprehension? How Do I Make The Loop Be The Same?
from my understanding shouldn't the print for X be the same on both? the answer i need is the one that comes from the list-comprehension, which is a new list where every element is
Solution 1:
First thing, list is a protected keyword. You should be using list_ at least (that's the naming convention if you really need to use list as the name).
The second iterates element by element, and prints each of the elements, what you want is in the loop to set each of the elements one by one, and then print x (not inside the loop).
list_= [1,2,3,4,5]
x=[]
for i in list_:
x.append(i-1)
print(x)
Post a Comment for "New To Learning Python; Why Is The Print For This For-loop Different When I Use A List Comprehension? How Do I Make The Loop Be The Same?"