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
87 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,210 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,720 points
111 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
169 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 in Python by Omkar
• 68,480 points
362 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 in Python by Nymeria
• 3,520 points
56 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 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 423 views
0 votes
1 answer

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

with open(fname) as f:     content = f.readlines() # you ...READ MORE

answered Oct 9, 2018 in Python by SDeb
• 13,210 points
143 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,210 points
105 views