How can you produce the following list with range() in Python?
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
reversed()function (efficient sincerangeimplements__reversed__):It’s much more meaningful.
Update:
listcastIf you want it to be a
list(as @btk pointed out):Update:
range-only solutionIf you want to use only
rangeto achieve the same result, you can use all its parameters.range(start, stop, step)For example, to generate a list
[3, 2, 1, 0], you can use the following:It may be less intuitive, but it works the same with less text. This answer by @Wolf indicates this approach is slightly faster than
reversed.