How to Reverse a list in Python

0 votes

I need to print the elements of an array from the end to the beginning.

For example, in this case:

array = [0, 10, 20, 40]
for (i = array.length() - 1; i >= 0; i--)

How do I reverse the list?

Sep 28, 2018 in Python by ana1504.k
• 7,890 points
209 views

1 answer to this question.

0 votes

You can use the reversed function in this case.

For example:

>>> array=[0,10,20,40]
>>> for i in reversed(array):
...     print(i)

The reversed(....) doesn't return a list though. You can get the reversed list by using list(reversed(array))

answered Sep 28, 2018 by SDeb
• 13,250 points

Related Questions In Python

0 votes
1 answer

How to Print a List in Python

ou are using Python 2.x syntax with ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,780 points
237 views
0 votes
1 answer

List comprehension on a nested list - How to do it in Python?

Not sure what your desired output is, ...READ MORE

answered Nov 21, 2018 in Python by Nymeria
• 3,520 points
590 views
0 votes
1 answer

How to list only text files in a directory using python?

Well, you are using a complex way. ...READ MORE

answered Feb 3, 2019 in Python by Omkar
• 69,030 points
1,174 views
0 votes
1 answer

How to zip with a list output in Python instead of a tuple output?

Good question - Considering that you are ...READ MORE

answered Feb 7, 2019 in Python by Nymeria
• 3,520 points
338 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,588 views
0 votes
0 answers
0 votes
2 answers

In Python, how do I read a file line-by-line into a list?

readline function help to  read line in ...READ MORE

answered Jun 21 in Python by sahil
• 540 points
316 views
+1 vote
1 answer

How to Profile a script in Python

Python includes a profiler called cProfile. It ...READ MORE

answered Nov 21, 2018 in Python by SDeb
• 13,250 points
528 views