I want to introduce another programming interview question for Python today: How to Reverse List. I am going to solve this problem by using a new technique I learned in Python called list comprehension. I also want to share my strategy for remembering new coding skills I learn using an evidence-based study technique called active recall and spaced repetition.
However, I am currently procrastinating by watching videos of cats getting stuck in things on YouTube. I find it funny that cats almost universally think walking in reverse will help them get out of their predicament of getting stuck in things.
I also resonate with the helplessness that cats feel when they get stuck in things. I feel the same feeling of helplessness when I learn new skills in coding only to forget it a few days later. My brain tends to reverse course as it defaults to using technique that I am familiar with. Even though, that might not be the best way of doing things. Now, let’s look at the problem of reversing list in Python using list comprehension and see how I can go about remembering the technique of list comprehension.
The Problem Statement
Implement a function Reverse(lst) that takes a given input list and returns an output of the original list in reverse order. Let’s look at a few samples of input and output to function:
Input | Output |
[5, 6, 7, 8, 9, 10] | [5, 4, 3, 2, 1, 1] |
[‘e’, ‘n’, ‘e’, ‘s’, ‘r’, ‘a’] | [‘a’, ‘r’, ‘s’, ‘e’, ‘n’, ‘e’] |
[‘n’, ‘i’, ‘p’, ‘u’, ‘l’] | [‘l’, ‘u’, ‘p’, ‘i’, ‘n’] |
The Solution
There is one super easy way to reverse a list. This what I use when I need an easy way to reverse a list. Just use the built-in function reverse that’s already implemented by the Python standard library! I know this from reading the official Python doc on the built-in data structures here. One caveat is that the built-in reverse function will reverse the elements of the list in place. Which means that the original list will be modified. I can’t use this if I don’t want to modify the original list.
Built-in Reverse Function
def Reverse(lst):
lst.reverse()
return lst
Another way to reverse a list in Python is to use list comprehension. List comprehension in Python is a syntactic sugar. A syntactic sugar in programming is syntax that is designed to make things easier to read or write. In this case, a list comprehension is simply a syntactic sugar for creating lists in situations where loops are usually used. List comprehension was introduced in the release of Python 2.0. PEP describes list comprehension in detail here.
Although list comprehension makes it easier to code; overusing list comprehension is a bad idea if there are complex logic in them that makes it hard to read and understand. As a rule of thumb, I use a list comprehension only when I need to create a list with simple logic. Otherwise, I will stick to using a for loop.
For Loop Implementation
Before coming across list comprehension, I would reverse a list by using a for loop like this:
def reverse_for_loop(lst):
reverse_lst = []
for i in range(len(lst)-1,0, -1):
reverse_lst.append(lst[i])
return reverse_lst
List Comprehension Implementation
Now, instead of using for loop, I can use list comprehension and an iterator to create another list in reverse order like this:
def Reverse(lst):
return [ele for ele in reversed(lst)]
That is much easier to read and write as it is very concise.
Active Recall and Spaced Repetition
Even though I find it intuitive to use list comprehension now, this is not always the case. In the beginning of my coding journey with Python, I keep forgetting how to use list comprehension and keep going back to using for loop. It turns out that my brain is not very good at retaining information just by reading and consuming information. It is much easier to remember new coding techniques I learn when I practice active recall and spaced repetition.
I first learned about the concept of active recall and spaced repetition from Ali Abdaal’s article and Barbara Oakley’s online course. They cover strategies on study techniques in much more depth. Sort of a meta study on how to study. I’ll give a quick primer on what the active recall and spaced repetition is. Active recall is basically a fancy way of saying retrieval of information from the brain through essentially repeated quizzing. Spaced repetition is spacing out when you quiz yourself. I find that I am much better at remembering coding techniques now by combining these two strategies together.
Anki
My favorite way of practicing active recall and spaced repetition by using Anki. Anki is a digital flashcard program that I use for quizzing myself on programming concepts. Consistent use of Anki has changed the game for me in terms of improving my memory. Anki is also available for free as an open-source software here
Takeaway
In programming since there is always a never-ending list of things to learn. I find it hard to remember programming concepts with the constant amount of information that I am feeding into my brain. I struggle to remember coding techniques in Python like list comprehension. This technique is especially useful when I need to build a list. However, I now find it easier to retain the programming techniques that I learn by using two powerful studying strategies: active recall and spaced repetition. My favorite way of applying these strategies is by using a digital flashcard program called Anki.