Skip to content
Advertisement

Print a list in reverse order with range()?

How can you produce the following list with range() in Python?

JavaScript

Advertisement

Answer

use reversed() function:

JavaScript

It’s much more meaningful.

Update:

If you want it to be a list (as btk pointed out):

JavaScript

Update:

If you want to use only range to achieve the same result, you can use all its parameters. range(start, stop, step)

For example, to generate a list [5,4,3,2,1,0], you can use the following:

JavaScript

It may be less intuitive but as the comments mention, this is more efficient and the right usage of range for reversed list.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement