Skip to content

How to Reverse a Python List (6 Ways)

How to reverse a Python List Cover Image

In this tutorial, you’ll learn how to use Python to reverse a list. This is a common task at hand (and a common Python interview question you’ll encounter). Knowing how to approach this task in different ways can help you choose the right method in different situations.

Python comes with a number of methods and functions that allow you to reverse a list, either directly or by iterating over the list object. You’ll learn how to reverse a Python list by using the reversed() function, the .reverse() method, list indexing, for loops, list comprehensions, and the slice method.

While learning six different methods to reverse a list in Python may seem excessive, one of the beautiful things about Python is the flexibility it affords. Some methods will fit better into your style, while others may be more performant or easier to read.

Let’s get started!

The Quick Answer: Use a Python’s .reverse()

# Using list.reverse() to reverse a list in Python
values = [1, 2, 3, 4, 5]
values.reverse()
print(values)

# Returns: [5, 4, 3, 2, 1]

Table of Contents

Summary: How to Reverse a List in Python

In this guide, we’ll cover six different ways of reversing a list in Python. However, let’s cover what the best and fastest way to reverse a list in Python is. From there, you can read about the various methods in the sections below.

The most Pythonic way to reverse a list is to use the .reverse() list method. This is because it’s intentional and leaves no doubt in the reader’s mind as to what you’re hoping to accomplish.

The fastest way to reverse a list in Python is to use either a for loop or a list comprehension. In order to test this, we tested each method on a list containing 100,000,000 items! This allowed us to really stress test each of the different methods.

Let’s take a look at what the results were for each of the methods:

MethodTime to reverse a 100,000,000 item list
.reverse() method32.6 ms ± 3.35 ms per loop
reversed() function680 ms ± 21.6 ms per loop
Reverse indexing386 ms ± 3.14 ms per loop
For loop751 ns ± 1.83 ns per loop
List comprehension747 ns ± 1.12 ns per loop
Slicer object382 ms ± 2.35 ms per loop
Different execution times of reversing a list in Python.

Now that we’ve covered that, let’s dive into how to use these different methods to reverse a list in Python.

Understanding Python List Indexing

Before we dive into how to reverse a Python list, it’s important for us to understand how Python lists are indexed. Because Python lists are ordered and indexed, we can access and modify Python lists based on their index.

Python list indices work in two main ways:

  1. Positive Index: Python lists will start at a position of 0 and continue up to the index of the length minus 1
  2. Negative Index: Python lists can be indexed in reverse, starting at position -1, moving to the negative value of the length of the list.

The image below demonstrates how list items can be indexed.

How Python list indexing works
How Python list indexing works

In the next section, you’ll learn how to use the Python list .reverse() method to reverse a list in Python.

Reverse a Python List Using the reverse method

Python lists have access to a method, .reverse(), which allows you to reverse a list in place. This means that the list is reversed in a memory-efficient manner, without needing to create a new object.

Let’s see how we can reverse our Python list using the .reverse() method.

# Reverse a Python list with reverse()
original_list = [1,2,3,4,5,6,7,8,9]
original_list.reverse()
print(original_list)

# Returns: [9, 8, 7, 6, 5, 4, 3, 2, 1]

Because the operation is handled in place, if you need to retain the original list, you’ll first need to create a copy of it. We can create a copy of the list by using the .copy() method on the original list first. Let’s take a look at how this works:

# Reverse a Python list with reverse()
original_list = [1,2,3,4,5,6,7,8,9]
reversed_list = original_list.copy()
reversed_list.reverse()

print('Original List: ', original_list)
print('Reversed List: ', reversed_list)

# Returns:
# Original List:  [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Reversed List:  [9, 8, 7, 6, 5, 4, 3, 2, 1]

We can see that by first applying the .copy() method to the original list, we can then chain in the .reverse() method to reverse the copied list.

In the next section, you’ll learn how to reverse a list by using list indexing.

Reverse a Python List Using the reversed function

Python has a built-in function, reversed(), which takes an iterable object such as a list and returns the reversed version of it.

The reversed() method works similar to the list indexing method covered below. It was added to Python to replicate the indexing method without being overly verbose.

Let’s take a look at how we can use the reverse() function to reverse a Python list:

# Reverse a Python list with reversed()
original_list = [1,2,3,4,5,6,7,8,9]
reversed_list = list(reversed(original_list))

print(reversed_list)

# Returns: [9, 8, 7, 6, 5, 4, 3, 2, 1]

The reversed() function actually returns an iterator object of type reverseiterator. Because of this, we need to pass the object into the list() function to convert it to a list.

We can verify this by writing the following code:

# Checking the Type of a reverseiterator
original_list = [1,2,3,4,5,6,7,8,9]
print(type(reversed(original_list)))

# Returns: <class 'list_reverseiterator'>

By passing the object into the list() function, all items are pulled into the list.

At its core, the iterator object is quite memory efficient. This is because it behaves like a generator object and evaluates lazily (only when an item is accessed). When we convert it to a list using the list() function, we lose this efficiency.

In the following section, you’ll learn how to use list indexing to reverse a Python list.

Reverse a Python List Using List Indexing

When indexing a list, people generally think of accessing all items between a start and an end position. However, you can also include a step variable that allows you to move across indices at different intervals. List indexing takes the form of [start:stop:step].

For example, a_list[::1] would return the entire list from beginning to end. If we, instead, stepped using a value of -1, then we would traverse the list from the last item to the first.

Moving over a list from end to the beginning is the same as reversing it. Let’s see how we can use Python’s list indexing to reverse a list:

# Reverse a Python list with list indexing
original_list = [1,2,3,4,5,6,7,8,9]
reversed_list = original_list[::-1]
print(reversed_list)

# Returns: [9, 8, 7, 6, 5, 4, 3, 2, 1]

In the code above, we create a new list by stepping over the original list in reverse order. This method doesn’t modify the object in place and needs to be assigned to a variable.

Reverse a Python List Using a For Loop

Python for loops are great ways to repeat an action for a defined number of times. We can use the reversed() and iterate over its items to create a list in reverse order.

Let’s see how this can be done:

# Reverse a Python list with a for loop
original_list = [1,2,3,4,5,6,7,8,9]
reversed_list = list()

for item in original_list:
    reversed_list = [item] + reversed_list

print(reversed_list)

# Returns: [9, 8, 7, 6, 5, 4, 3, 2, 1]

Let’s break down what the code block above is doing:

  1. We define two lists, our original list and an empty list to store our reversed items
  2. We then loop over each item in the list. We reassign the reversed list to a list containing each item plus the reversed list.

In the next section, you’ll learn how to use a Python list comprehension in order to reverse a list.

Reverse a Python List Using a List Comprehension

Similar to using a for loop for this, we can also use a list comprehension to reverse a Python list. Rather than simply converting the for loop to a list comprehension, however, we’ll use a slightly different approach to accomplish our goal.

We will iterate over each item in our original list in the negative order. Let’s take a look at what this looks like:

# Reverse a Python list with a List Comprehension
original_list = [1,2,3,4,5,6,7,8,9]
start = len(original_list) - 1
reversed_list = [original_list[i] for i in range(start, -1, -1)]
print(reversed_list)

# Returns: [9, 8, 7, 6, 5, 4, 3, 2, 1]

Let’s break down what the code above is doing:

  1. We create a variable start that represents the length of the list minus one
  2. We then create a list comprehension that accesses each index item in our list using the range from the start position to 0 in reverse order.

This method works, but it’s not the most readable method. It can be a good way to learn how to do this for an interview, but beyond this, it’s not the most recommended method.

Reverse a Python List Using the Slice Method

Python also provides a slice() function that allows us to index an object in the same way that the list indexing method works above. The function creates a slice object that allows us to easily reuse a slice across multiple objects. Because of this, we could apply the same indexing across different lists.

Let’s see how we can use the function in Python:

# Reverse a Python list with slice()
original_list = [1,2,3,4,5,6,7,8,9]
reverser = slice(None, None, -1)
reversed_list = original_list[reverser]
print(reversed_list)

# Returns: [9, 8, 7, 6, 5, 4, 3, 2, 1]

The benefit of this approach is that we can assign a custom slice that can be easily re-used, without needing to modify multiple items. This allows you to easily apply the same slice to multiple items.

In addition to this, the slicer() function allows you to give the slice an easy-to-understand name. In the example above, we named the slicer reverser in order to make it clear what the slice object is doing.

Frequently Asked Questions

What is the best way to reverse a list in Python?

The best and most Pythonic way to reverse a list in Python is to use the list.reverse() method. This method reverses the list in place and is the clearest way to communicate to your reader what you’re doing.

How do you reverse a list in Python using slicing?

The best way to reverse a list using slicing in Python is to use negative indexing. This allows you to step over a list using -1, by using the code list[::-1].

What is the fastest way to reverse a list in Python?

The fastest way to reverse a list in Python is to use a for loop (or a list comprehension). This method is hundreds of times faster than other methods (even when tested against lists with 100,000,000 items).

Conclusion

In this tutorial, you learned how to reverse a list in Python. You learned how list indexing works and how this can be used to reverse a list. You then learned how to use the reverse() and reversed() methods to reverse a list. Then, you learned how to use for loops and list comprehensions, as well as the slice function.

Additional Resources

To learn more about related topics, check out the tutorials below:

  • Python List sort(): An In-Depth Guide to Sorting Lists
  • How to Iterate (Loop) Over a List in Python
  • Python: Find List Index of All Occurrences of an Element
  • Python: Reverse a Number (3 Easy Ways)
  • To learn more about lists in Python, check out  the official documentation here.

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials. View Author posts

Tags: Python Python Lists
  • Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Python in 30 days cover image

    Learn Python in 30 days. For free.

    .

    两个鬼故事杨志后面起一个名字起名字大全女孩打分测试评分标准ou99.comApex玩家数量两个字姓王的男孩起名美国苹果官网绿色生态的商标起名私募排行姓牛男幼儿起名古诗宋词取名女孩起名烜字起名好不好大蛇无双2下载qq空间fd60坪恐怖游轮百度影音原木护墙板中华武功生僻字歌曲歌词带拼音人美b岳风柳萱刚刚更新免费阅读全部花开花落终有时起家园林公司名字大全男孩子起名字姓吴的三藏取名起名大全网免费取名公司起名森配什么字好带瑞字的起名男孩名字甄嬛传全集在线观看百度影音扶贫公司名字起名大全起名大全邓姓男孩第二处女少年生前被连续抽血16次?多部门介入两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”淀粉肠小王子日销售额涨超10倍高中生被打伤下体休学 邯郸通报单亲妈妈陷入热恋 14岁儿子报警何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言张家界的山上“长”满了韩国人?男孩8年未见母亲被告知被遗忘中国拥有亿元资产的家庭达13.3万户19岁小伙救下5人后溺亡 多方发声315晚会后胖东来又人满为患了张立群任西安交通大学校长“重生之我在北大当嫡校长”男子被猫抓伤后确诊“猫抓病”测试车高速逃费 小米:已补缴周杰伦一审败诉网易网友洛杉矶偶遇贾玲今日春分倪萍分享减重40斤方法七年后宇文玥被薅头发捞上岸许家印被限制高消费萧美琴窜访捷克 外交部回应联合利华开始重组专访95后高颜值猪保姆胖东来员工每周单休无小长假男子被流浪猫绊倒 投喂者赔24万小米汽车超级工厂正式揭幕黑马情侣提车了西双版纳热带植物园回应蜉蝣大爆发当地回应沈阳致3死车祸车主疑毒驾恒大被罚41.75亿到底怎么缴妈妈回应孩子在校撞护栏坠楼外国人感慨凌晨的中国很安全杨倩无缘巴黎奥运校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变王树国卸任西安交大校长 师生送别手机成瘾是影响睡眠质量重要因素国产伟哥去年销售近13亿阿根廷将发行1万与2万面值的纸币兔狲“狲大娘”因病死亡遭遇山火的松茸之乡“开封王婆”爆火:促成四五十对奥巴马现身唐宁街 黑色着装引猜测考生莫言也上北大硕士复试名单了德国打算提及普京时仅用姓名天水麻辣烫把捣辣椒大爷累坏了

    两个鬼故事 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化