'Reverse a List in Python' Tutorial: Three Methods & How-to Demos

preview_player
Показать описание

In this Python tutorial video you'll see how to reverse a list in Python. There are 3 common implementations for a Python reverse list algorithm and we'll do a deep dive on all of them. Here's what we'll cover with overviews and hands on REPL demos:

2. Using a list slicing "trick" to create a reversed copy of a Python list. You can use Python's list slicing syntax to create a reversed copy of a list: `my_list[::-1]`. This works well, however it is slightly arcane and therefore not very Pythonic, in my opinion.

3. Using Python's built-in "reversed()" function to create a reverse iterator that we can then turn into a proper list object. This is a clean solution that uses some advanced Python features like iterators—but it's also very readable due to the naming of the "reversed" function. This is a great option if you're looking to reverse a for loop and you don't need a full copy of the original list.

Note that there are other approaches like implementing list reversal from scratch or reversing a list using a recursive algorithm that are common interview questions but not very good solutions for Python programming in the "real world".

Watch the full video for details and hands-on demos for each approach. At the end of the video you'll see my final verdict on which list reversal approach I recommend and why.

* * *

FREE Python Tutorials & News:
Рекомендации по теме
Комментарии
Автор

Best I have seen so far. Thanks for breaking this down

cr
Автор

You are amazing, eloquent and have good communication skills, Thank you

sibusisoharvey
Автор

The reversed iterator however is sensitive to modify/delete of an item in the list while iterating.

>>> mylist = [1, 2, 3, 4, 5]
>>> rev = reversed(mylist)
>>> next(rev)
5
>>> next(rev)
4
>>> mylist.remove(3)
>>> next(rev)
4
>>> next(rev)
2

zomaarwat
Автор

I don't know if it's because I am crazy or because a sighted person's brain works differently from a blind person's one, but I liked the second method because, considering that everything in python is an object, I imagine myself touching the list items as if they were small boxes. So, knowing that the :: takes me to the end of the list (or of a string), since the first and second values are omitted, I am in the end of the list or string, so the -1 that comes after it is kind of intuitive.
But it was fun to learn the various methods to reverse a list! Thanks!

P.S.: There is even a free screen reader for Windows programmed in Python, so it helps us a lot. I guess our biggest problem is with indentation, since they are not spoken unless we spell the code!

supermalavox
Автор

It's funny. I think list slicing is a very obvious syntax for any user of the numpy/scipy stack. But I get it when you say it's not very pythonic. Was it promoted by some former Fortran users back in the day? Anyway, I think this helps me and others to understand and bridge the gap between science and software development. Keep up he good work!

teoguso
Автор

Nice Explanation.
It appears, the reversed function works a bit differently on a string than on a list. Could you please explain how that happens?

labtest
Автор

Very nice explanatiin.
Will you please tell me How to creat a REST API with any framework (Flask, cherrypy, Django, etc., ). Create a simple PY calculator that is accessible via GET and POST method.

liladharlingayat
Автор

hope this trik also help someone of list comprensation

lis = [item for item in reversed(list1)]
print(lis)

aishwarysaxena
Автор

Bro can you explain why do we use -1 in reverse

karna
Автор

How about this :
ls = [1, 2, 3, 4, 5]
ls2 = []
for i in ls:
ls2.insert(0, i)

print(ls2)

Gentlemanek
Автор

method #3 seems more complicated for a beginner

leonardohernandez
Автор

How to get a list input.. Like this [1, 2, 3, 4] as an input and print it like [4, 3, 2, 1]

swethamk
Автор

for loop se kar ke dikhaeye without function ka

jayprakashpatel
Автор

That thing in the interpreter... the one with a docstring... bepython? beepython? beerpython?

wintersilence
Автор

I hate these people never link the playlist.

Juanbaez_
visit shbcf.ru