How to reverse a list

0 votes

How to do this in Python?

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

I need to have the elements of an array, but from the end to the beginning.

Feb 11, 2022 in Python by Nandini
• 5,480 points
458 views

1 answer to this question.

0 votes

A  list in Python can be reversed by either using reverse or using slicing.

container = [0,10,20,40]
container.reverse()
container

Output

[40, 20, 10, 0]
container = [0,10,20,40]
container[::-1]  #[start:stop:step]

Output

[40, 20, 10, 0]
answered Feb 11, 2022 by Dev
• 6,000 points

Related Questions In Python

0 votes
1 answer

How to Reverse a list in Python

You can use the reversed function in ...READ MORE

answered Sep 29, 2018 in Python by SDeb
• 13,300 points
818 views
0 votes
1 answer

How to sort a list of strings?

Basic answer: mylist = ["b", "C", "A"] mylist.sort() This modifies ...READ MORE

answered Jun 4, 2018 in Python by aryya
• 7,460 points
1,480 views
0 votes
1 answer

How to sort a list of strings?

Try  items = ["live", "like", "code", "cool", "bug"] ...READ MORE

answered Jul 27, 2018 in Python by Priyaj
• 58,100 points
827 views
0 votes
1 answer

How to split a string into a list?

You can use the function  text.split() This should be ...READ MORE

answered Jul 30, 2018 in Python by Priyaj
• 58,100 points
857 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,397 views
0 votes
1 answer
0 votes
1 answer

How to convert list to string

To convert list to string in Python ...READ MORE

answered Feb 9, 2022 in Python by Dev
• 6,000 points
477 views
0 votes
1 answer

How do I get the number of elements in a list

items = [] items.append("apple") items.append("orange") items.append("banana") len(items) #use the ...READ MORE

answered Feb 15, 2022 in Python by Dev
• 6,000 points
653 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP