batteriesinfinity.com

Mastering Python Lists: Sorting and Reversing Techniques

Written on

Chapter 1 Introduction to Python List Management

In this section, we will delve into various techniques for organizing lists in Python. Specifically, we will explore the sort() method, the sorted() function, and the reverse() method.

Traveling soon? Avoid those expensive roaming fees with Saily eSIM from NordVPN and NordPass Creators.

Chapter 1.1 Using the sort() Method

Let's consider a list of friends illustrated below:

friends = ['Shelly', 'Dale', 'Laura', 'Harry']

Applying the sort() method allows us to arrange this list in alphabetical order:

friends.sort()

print(friends)

Output:

['Dale', 'Harry', 'Laura', 'Shelly']

If we wish to sort the list in reverse alphabetical order, we can use the following:

friends.sort(reverse=True)

print(friends)

Output:

['Shelly', 'Laura', 'Harry', 'Dale']

Note that the sort() method modifies the list in place, making the changes permanent.

Chapter 1.2 Utilizing the sorted() Function

For those who want to maintain the original order while temporarily displaying the list sorted, the sorted() function is ideal:

friends = ['Shelly', 'Dale', 'Laura', 'Harry']

print(sorted(friends))

Output:

['Dale', 'Harry', 'Laura', 'Shelly']

To view the list in reverse order:

print(sorted(friends, reverse=True))

Output:

['Shelly', 'Laura', 'Harry', 'Dale']

The original list remains unchanged:

print(friends)

Output:

['Shelly', 'Dale', 'Laura', 'Harry']

Chapter 1.3 The reverse() Method

We can reverse the order of our list using the reverse() method:

friends.reverse()

print(friends)

Output:

['Harry', 'Laura', 'Dale', 'Shelly']

To restore the original order, simply apply the reverse() method again:

friends.reverse()

print(friends)

Output:

['Shelly', 'Dale', 'Laura', 'Harry']

Chapter 1.4 Checking List Length with len()

To determine the number of elements in a list, the len() function is useful:

friends1 = ['Shelly', 'Dale', 'Laura', 'Harry']

len(friends1)

Output:

4

For another list:

friends2 = ['Donna', 'Dale', 'Shelly', 'Laura', 'Harry']

len(friends2)

Output:

5

References

[1] Python Crash Course 3rd ed., Eric Matthes, (shop online & buy the book).

[2] Python Programming and Computer Programming

Support me at no additional cost and even save money when you shop online: visit My Affiliate Storefront for 50% Off Promo Codes. Secure Your Network and Online Privacy with Industry Leading Services like NordVPN (69% Off limited time deal) and NordPass Premium (50% Off limited time offer), complete with a 30-day money-back guarantee! Get Deals Now!

Photo by Markus Winkler on Unsplash

Optimism is a perfectly legitimate response to failure.

— Stephen King

This post contains affiliate links. If you click on any of them and make a purchase, I may earn a commission at no extra cost to you. Please consider supporting me with as little as $1 (No sign-ups required! Just Tap & Tip).

Chapter 2 Additional Learning Resources

To further your knowledge in Python programming, check out these helpful videos.

The first video titled "Start Python 3 Programming Today! Part 2" provides a comprehensive introduction to Python programming.

The second video, "Python Tutorial for Beginners | Python 3 Tutorial [2021]," offers essential insights for those new to Python.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Reflecting on 2023: A Personal Journey Towards Growth

As 2023 concludes, I reflect on personal challenges and growth, setting the stage for a transformative 2024.

Mastering Python: Enhance Your Code with Match Statements & Slicing

Discover how match statements and slicing can improve your Python code's clarity and efficiency.

Understanding NASA’s Upcoming Moon Rover and Its Significance

Explore NASA's ice-hunting mission with VIPER and its potential impact on lunar exploration and human presence on the moon.