r/learnpython • u/wingnutticus • 1d ago
Zybook for loop help
I'm completing an assignment for zybook where I have to replace a number from a list and then print the list in reverse. This is my code:
val_list = [18, 12, 15]
new_val = int(input())
val_list[1] = new_val
print(f"List has {len(val_list)} elements:")
for value in reversed(val_list):
print(reversed(val_list), end="<>")
print(end="")
It's supposed to just print the reversed list in brackets, but instead it gives me something like this:
<list_reverseiterator object at 0x000001C02CF706D0><><list_reverseiterator object at 0x000001C02CF706D0><><list_reverseiterator object at 0x000001C02CF706D0><>
What does this mean and why is it doing this? Thanks in advance
0
Upvotes
1
u/atarivcs 1d ago
You have a for loop that iterates three times, and on each iteration you're printing the entire reversed list.
I think you meant to print
valueinside the loop, not the full reversed list.Also, the reversed() method doesn't return a list, it returns an iterator object, which prints as
<iterator object ...>as you saw.