'How to print a list in a reversed order using only one for loop in python?
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 :(
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Riccardo Bucco |
