Python Reverse a List: A Step-by-Step Tutorial
Tags: python, programming, codenewbie, tutorial
Published: 2021-09-14 03:55:13

---
title: Python Reverse a List: A Step-by-Step Tutorial
published: true
date: 2021-09-13 16:20:44 UTC
tags: #python #programming #codenewbie #tutorial
canonical_url: https://itsmycode.com/python-reverse-a-list-a-step-by-step-tutorial/
---

ItsMyCode |

Reversing a list is a common requirement in any programming language. In this tutorial, we will learn the effective way to reverse a list in Python. 

There are 3 ways to reverse a list in Python.

1. **Using the reversed() built-in function**
2. **Using the reverse() built-in function**
3. **Using the list slicing **

## Method 1 – Using the `reversed()` built-in function

**`reversed()` **is a built-in function in Python. In this method, we neither modify the original list nor create a new copy of the list. Instead, we will get a reverse iterator which we can use to cycle through all the elements in the list and get them in reverse order, as shown below.

**Output**

```python 
# Reversing a list using reversed()
def reverse_list(mylist):
	return [ele for ele in reversed(mylist)]
	

mycountrylist = ['US','India','Germany','South Africa']
mynumberlist = [1,2,3,4,5,6]

print(reverse_list(mycountrylist))
print(reverse_list(mynumberlist))

['South Africa', 'Germany', 'India', 'US']
[6, 5, 4, 3, 2, 1]
```

If we need a copy of the reversed list, we could use the below code to perform this operation.

```python 
mynumberlist = [1,2,3,4,5,6]
newlist = list((reversed(mynumberlist)))
print(newlist)

# Output
# [6, 5, 4, 3, 2, 1]
```

## Method 2 – Using the reverse() built-in function

**`reverse()` **is a built-in function in Python. In this method, we will not create a copy of the list. Instead, we will modify the original list object  **_in-place_**. It means we will copy the reversed elements into the same list. 

The **`reverse()`** method will not return anything as the list is reversed  **_in-place_**. However, we can copy the list before reversing if required.

```python 
# Reversing a list using reverse()
def reverse_list(mylist):
	mylist.reverse()
	return mylist

mycountrylist = ['US','India','Germany','South Africa']
mynumberlist = [1,2,3,4,5,6]

print(reverse_list(mycountrylist))
print(reverse_list(mynumberlist))

```

**Output**

```python 
['South Africa', 'Germany', 'India', 'US']
[6, 5, 4, 3, 2, 1]
```

## Method 3 – Using the list slicing 

**_Slice_** notation allows us to slice various collection objects such as **lists, strings, tuples, and Numpy Arrays**.

The **_slicing_** trick is the simplest way to reverse a list in Python. The only drawback of using this technique is that it will create a new copy of the list, taking up additional memory.

```python 
# Reversing a list using slicing technique
def reverse_list(mylist):
	newlist= mylist[::-1]
	return newlist

mycountrylist = ['US','India','Germany','South Africa']
mynumberlist = [1,2,3,4,5,6]

print(reverse_list(mycountrylist))
print(reverse_list(mynumberlist))
```

**Output**

```python 
['South Africa', 'Germany', 'India', 'US']
[6, 5, 4, 3, 2, 1]
```

The post [Python Reverse a List: A Step-by-Step Tutorial](https://itsmycode.com/python-reverse-a-list-a-step-by-step-tutorial/) appeared first on [ItsMyCode](https://itsmycode.com).