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 -1 from the original list. But the for-loop one only gives 1 element, I also don't know how that element is calculated. Printing x just gives the last element of the list. I'm sure i'm doing something wrong but i'm not sure how to get a list from just using the for-loop. WHAT IS CONFUSING ME is that if the print(x) is part of the for loop it will print the elements of the desired list I need, but NOT in a list, which means the math I wrote works as intended, right?
list= [1,2,3,4,5]
#loop
x=[]
for i in list:
x=[i-1]
print(x)
#list comprehension
x=[i-1 for i in list]
print(x)
#confusing part where this print will be the same as the comprehension but not in a list form
x=[]
for i in list:
x=[i-1]
print(x)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…