Python List Reversal vs List Sorting: Which One Should You Use and Why?

Vidya Gopinath for keySkillset Vidya Gopinath for keySkillset
Vidya Gopinath for keySkillset
23
June
2023
Python List Reversal vs List Sorting: Which One Should You Use and Why?

Reversing and sorting lists are fundamental operations in Python, widely used for data manipulation and analysis. Reversing a list involves changing the order of elements, while sorting arranges them in a specific order. These operations are crucial for tasks such as examining data in reverse chronological order, finding minimum or maximum values, or organizing data for efficient searching.

 In this blog, we'll explore how to reverse and sort lists in Python while preserving the original order, covering various examples and scenarios. By mastering these techniques, you'll gain valuable skills for effective list management and data manipulation in Python. 

When working with lists in Python, there may be situations where you need to reverse the list or sort it without modifying the original list. Python provides built-in functions like reverse() and sort() for this purpose, but they modify the list in place. To maintain the original order, you can use the reversed() and sorted() functions. In this article, we will explore how to reverse and sort a list using these list operations while preserving the original list.

Reversing a List:

To reverse a list in Python, you can use the reversed() function, which returns an iterator that produces the reversed elements of the list. To maintain the original list, you can convert the iterator into a new list using the list() function.

 Here's an example:

l = [2, 5, 8, 6, 3, 4, 7, 9]

rev = list(reversed(l))

print(rev)  

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

See, the screenshot below: 

Example 1: Using sorted() - Sorting a list of numbers in reverse order

numbers = [5, 2, 9, 1, 7, 3]

sorted_numbers = sorted(numbers, reverse=True)

print(sorted_numbers)  

# Output: [9, 7, 5, 3, 2, 1]

Screenshot is below:

Example 2: Using sort() - Sorting a list of strings in reverse order

fruits = ['apple', 'banana', 'orange', 'grape', 'kiwi']

fruits.sort(reverse=True)

print(fruits) 

 # Output: ['orange', 'kiwi', 'grape', 'banana', 'apple']

Here is the screenshot 

Example 3: Using sorted() - Sorting a list of tuples based on a specific element in reverse order

students = [('John', 25), ('Alice', 20), ('Bob', 22), ('Jane', 27)]

sorted_students = sorted(students, key=lambda x: x[1], reverse=True)

print(sorted_students)

# Output: [('Jane', 27), ('John', 25), ('Bob', 22), ('Alice', 20)]

Check this screenshot: 

Example 4: Using sort() - Sorting a list of dictionaries based on a specific key in reverse order

books = [

    {'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald', 'year': 1925},

    {'title': 'To Kill a Mockingbird', 'author': 'Harper Lee', 'year': 1960},

    {'title': '1984', 'author': 'George Orwell', 'year': 1949}

]

books.sort(key=lambda x: x['year'], reverse=True)

print(books)

# Output: [{'title': 'To Kill a Mockingbird', 'author': 'Harper Lee', 'year': 1960},

#          {'title': '1984', 'author': 'George Orwell', 'year': 1949},

#          {'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald', 'year': 1925}]

Screenshot is here: 

These examples demonstrate different scenarios where you can utilize the sorted() and sort() functions to sort lists in reverse order, whether they contain numbers, strings, tuples, or dictionaries.

Sorting a List:

To sort a list in Python, you can use the sorted() function. By default, it sorts the list in ascending order. If you want to sort the list in descending order, you can specify the reverse=True parameter. Similar to the reversed() function, the sorted() function returns a new sorted list while preserving the original list. Here's an example:

l = [2, 5, 8, 6, 3, 4, 7, 9]

srt = sorted(l)

print(srt)  # Output: [2, 3, 4, 5, 6, 7, 8, 9]

srt_reverse = sorted(l, reverse=True)

print(srt_reverse)  # Output: [9, 8, 7, 6, 5, 4, 3, 2]

The sort() method is used to sort the elements of a list in a specific order, either ascending or descending.

To sort the elements in ascending order, you can use the following syntax:

list.sort()

Example:

students = ['Harsh', 'Andrew', 'Danny']

students.sort()

print(students)

# Output: ['Andrew', 'Danny', 'Harsh']

Screenshot is here: 

In the above example, the sort() method is used to sort the students list in ascending order, resulting in ['Andrew', 'Danny', 'Harsh'].

To sort the elements in descending order, you can use the following syntax:

list.sort(reverse=True)

Now let's see how to sort a list in descending order using the sort() method:

students = ['Harsh', 'Andrew', 'Danny']

students.sort(reverse=True)

print(students)

# Output: ['Harsh', 'Danny', 'Andrew']

Find the screenshot: 

In this case, the sort() method with the reverse=True argument is used to sort the students list in descending order, resulting in ['Harsh', 'Danny', 'Andrew'].

It's important to note that the sort() method modifies the order of the list permanently. If you want to maintain the original order of the list while displaying it in a specific order, you can use the sorted() function.

Example:

students = ['Harsh', 'Andrew', 'Danny']

sorted_students = sorted(students)

print(sorted_students)

print(students)

# Output: ['Andrew', 'Danny', 'Harsh']

#         ['Harsh', 'Andrew', 'Danny']

Check out this screenshot: 

In the above example, the sorted() function is used to sort the students list in ascending order without modifying the original order of the list. The output displays ['Andrew', 'Danny', 'Harsh'], while the students list remains unchanged as ['Harsh', 'Andrew', 'Danny'].

Conclusion:

In Python, you can reverse a list using the reversed() function and sort a list using the sorted() function. These functions allow you to maintain the original order of the list while obtaining the reversed or sorted version. By understanding these list operations, you can effectively manipulate lists in Python without modifying the original list.

And if you are looking to upskill in Python coding, you may want to check the free access we have at keySkillset.

Click here to view the resourceClick here to download the resource
Begin your simulation journey today

Start learning new skills with the help of KeySkillset courses and our learning management system today!