Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the list reverse() method in Python?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The list.reverse() method is simply used to reverse the order of the items in a list in Python.

Syntax

list.reverse()

Parmeter value

There are no parameter values.

Example 1

Let’s create a list of even numbers up to ten and then use the list.reverse() method to reverse the order of the items in the list.

Code

even_numbers = [2, 4, 6, 8, 10]
# now let us reverse the items in the list
even_numbers.reverse()
print(even_numbers)

Explanation

  • Line 1: We created a list of even numbers up to ten and we named it even_numbers.
  • Line 4: We applied the list.reverse() method to reverse the items on the list using even_numbers.reverse().
  • Line 5: We returned the output.

Example 2

Let’s reverse the order of items present in a list of countries.

Code

countries = ['Argentina', 'Niger', 'Italy', 'Russia', 'Australia']
# reversing the order of the items in the list
countries.reverse()
print(countries)

RELATED TAGS

python
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring