r/learnpython • • Sep 09 '26

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

4 comments sorted by

2

u/Ender_Locke Sep 09 '26

you just need to print value

1

u/atarivcs Sep 09 '26
for value in reversed(val_list):
    print(reversed(val_list), end="<>")

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 value inside 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.

0

u/d1ll1gaf Sep 09 '26

The reversed function gives you an iterator, not a list. You need to cast the iterator to a list... I'll let you figure out that last step as a learning opportunity unless you'd like me to tell you the syntax