Skip to main content

How to reverse a list in Python

How to reverse a list in Python

Knowing how to reverse a list in Python is a trick that can come in handy surprisingly often. In this post, we’ll look at a few ways you can do it!

How to reverse a list in Python the easy way

In Python, a list is a variable that stores multiple other variables. This is useful if you ever need to store a list of contacts, a list of options, or anything else in your app. Check out our detailed guide to using lists in Python here:

The good news is that reversing lists in Python is extremely simple. That’s because there just so happens to be a method built-in to Python that will do it for you instantly. And it has a logical name, too: reverse().

Simple take your list then use the method:

listOfNames.reverse()

From this point on, your list will now be reversed!

But what if you don’t want to permanently jumble up your list? In that case, you can retrieve a reversed version of your list very simply too:

reversed(listOfNames)

Now you can say something along the lines of:

listOfNames = ["Bob", "Janine", "Janine", "Bart"]
for n in reversed(listOfNames):
    print(n)

How to reverse a list in Python with slicing

Another option for reversing your list is to use slicing. This method is a little less efficient because it creates a new copy of the list, which will take up additional memory.

You use slicing with the following syntax:

print(listOfNames[::-1])

This little trick works in the same way that we’re able to get a range from a list. To print just the middle two elements in a list, for instance, we could say:

print(listOfNames[1:3])

(Remember that a list starts with the index “0” – so “1” is actually the second item! Likewise, the second number is non-inclusive).

This is essentially using a backwards range in order to do the same thing!

How to reverse a list in Python manually

While there are not many scenarios where it would be necessary, it’s also possible to use a similar method to reverse a list manually:

for n in reversed(listOfNames):

    newList.append(n)

This could potentially be useful if you need to check the values as you create the new string, or if you need to manipulate them in some other way.


So there you have it! That’s how to reverse a list in Python. Pretty simple!

This is just one of countless useful operations to be learned in Python. With each new skill, you’ll find your code becomes cleaner and more powerful! Why not extend your knowledge further with an online course? We compiled a list of the best online Python courses, which you can check out here:



source https://www.androidauthority.com/how-to-reverse-a-list-in-python-1138880/

Comments

Popular posts from this blog

5 tips to Voice Speech Recognition in Android Marshmallow

Android Marshmallow landed on our Android devices. The opportunity for us to offer a small compilation of tricks to try immediately. The latest version of the Google OS starts (very gently, unhurriedly) to be offered on other devices as Nexus. You will find on Android TM, in the coming weeks, a compilation of the best tips for Android 6.0 Marshmallow. It starts slowly with a video featuring the 10 essential tips & tricks of this new version of the little green robot OS. To continue, we offer a selection of five "tricks" on the management of the battery on Android 6.0 Marshmallow. To enjoy longer your newly updated mobile. Follow the guide. then continue with 5 tips to tame the super-assistant Google Now on Tap. Here you will find 5 "tips" to manage in the best way your applications. We then discuss the quick tips to navigate more easily on this version of the Google OS. We enchanters with features focused on safety and the protection of personal data. We co...

Energy Android TV Play turns your TV into a Smart TV

ENERGY SISTEM Android Energy TV Play, you have a smart TV with Android operating system allows you to convert any traditional TV has announced the launch of a new product. Energy Android TV Play can be connected to the TV to enjoy f the size of a flash drive, a smart phone, a tablet and a computer unconsolidated is a lightweight device. System 1.6 GHz, DDR3 1GB of RAM and a dual-core processor can be expanded using external USB devices, which is the internal memory of 8 GB. It also integrates WiFi and a USB port for connecting external devices. One of its outstanding features, it is easily connected to the TV screen by screen cast application to display the contents of any terminal, making any phone or tablet is synchronized with iOS or Android. All ENERGY SISTEM products one click In addition, through streaming media service applications, images, video or other multimedia content, and game play is the ability to share. With integrated WiFi, the device you want from t...

How to run Python apps on any platform

Credit: Adam Sinicki / Android Authority Want to know how to run Python? It sounds simple, but it can actually be tricky to figure this out. In this post, we’ll discuss how to test your Python code, as well as how to run Python in other contexts: online for example, or as a packaged app. Sometimes, the thing holding you back from learning to code can be extremely simple. I remember wanting to learn to program when I was younger – or learning to take what I’d learned from BASIC on the ZX Spectrum and apply that to a modern environment. My problem? I didn’t know “where” to program. Once I understood C# or Java, where would I enter the code and how would I run it? And whenever I asked someone, they would look at me blankly. What kind of a question is that? Thing is, I had never needed an IDE or an interpreter before. Machines like the ZX Spectrum and Tatung Einstein (any other Einstein users out there?) simply booted up with a prompt to code into! Many people have a similar iss...