Why is the value 3

Screen Link:

My Code:

""" values = [5, 4, 7, 8, 9, 3]
values_copy = []
for v in values:
    values_copy = v
    answer = values_copy"""

What I expected to happen:
why is the value of v = 3

What actually happened:

Replace this line with the output/error

Do you currently have an understanding of how for loops in Python work?

yes i do . i just dont understand why they took the value of the last element in the list.

Because you are looping through the list. And the last element of the list is a 3. So, for the final iteration, the value of v would be 3.

1 Like

In addition to above reply because you are not appending the result in the empty list after each iteration, you just assigned the " values_copy = v ", in this case which will be the last value of the list ‘values’, so if you want all values in list you should modify your code like this:

values = [5, 4, 7, 8, 9, 3]
values_copy = []
for v in values:
   values_copy.append (v)
   answer = values_copy
print(answer)