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

Показать описание
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:
Комментарии