

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to reverse the objects in a list in Python?
You can use the reverse method from the list class to reverse a list in place.
example
a = [3, "Hello", 2, 1] a.reverse() print(a)
Output
This will give the output −
[1, 2, "Hello", 3]
You can also use list slicing with the index as [::-1] If you want a new list to be made instead of reversing in place. This means take the start and stop as start and end of list and step as -1.
example
a = [3, "Hello", 2, 1] print(a[::-1])
Output
This will give the output −
[1, 2, "Hello", 3]
If you want a new list to be made instead of reversing in place, you can also use the reversed method.
example
a = [3, "Hello", 2, 1] rev_a = list(reversed(a)) print(rev_a)
Output
This will give the output −
[1, 2, "Hello", 3]
- Related Questions & Answers
- How to sort the objects in a list in Python?
- How to append objects in a list in Python?
- How to shuffle a list of objects in Python?
- Program to reverse a linked list in Python
- Python program to Reverse a range in list
- Program to reverse a list by list slicing in Python
- Reverse Linked List in Python
- How to Reverse a linked list in android?
- How to reverse a string in Python?
- How to reverse a number in Python?
- Python program to sort and reverse a given list
- Reverse each tuple in a list of tuples in Python
- Program to reverse inner nodes of a linked list in python
- How to access Python objects within objects in Python?
- Python Program to Display the Nodes of a Linked List in Reverse using Recursion
Advertisements