I have to define a function that prints out the list in reversed order. I'm not allowed to use str(), reversed(), .reverse or slicing method, and I must use only 1 for in loop. It's just about printing it reversed so the output printed vertically doesn't matter. I'm so lost please help :(
Asked
Active
Viewed 111 times
-2
-
Why don't you use a loop over the indices from the last one in the list down to 0? – mkrieger1 Apr 15 '22 at 10:54
-
1Does this answer your question? [Reverse a string in Python](https://stackoverflow.com/questions/931092/reverse-a-string-in-python) (this answer: https://stackoverflow.com/a/27401625) – mkrieger1 Apr 15 '22 at 10:55
-
Sorry, somehow I thought you were asking about a string, not a list, but the answer is much the same: https://stackoverflow.com/questions/529424/traverse-a-list-in-reverse-order-in-python – mkrieger1 Apr 15 '22 at 10:58
1 Answers
0
Try this (lst
is the list you want to reverse):
lst = [0, 1, 2, 3, 4]
print(*(lst[len(lst) - idx - 1] for idx in range(len(lst))))
The previous code prints
4 3 2 1 0

Riccardo Bucco
- 13,980
- 4
- 22
- 50