Reverse the List in Python: Interview Series, Part 1

Reverse the List in Python: Interview Series, Part 1

Place where I failed during my interview and my learnings after that

Hello Everybody! In this article, I am sharing how to reverse the list in Python. Recently I started hunting for a new job after a career break. I will share things that I learned during my interview experience as an interview series.

This is the first article of that series and I forgot how to reverse the list during an online coding round with the interviewer watching the screen. During the interview when someone is watching the code that you are writing there is a great chance that you commit silly mistakes. I become extra cautious during online coding interviews and sometimes I freeze if I couldn't get the answer right. I really love the quote that my friend Bhavani puts in her digital garden,

The brain is a sacred space meant for thinking not remembering. I'm big on using notes as my second brain

It's very difficult for me to retrieve the right syntax or built-in keyword from my memory during the interview process. While I could remember there is a way to do it, I get stuck in remembering the right keyword. There is one such scenario that happened where I forgot to reverse the list. After my interview process, I went through different ways, and here I am writing this article for everyone to let know of different ways to reverse the list. This way I am adding it to my second brain too.

The first way

Now back to the topic, of how to reverse the list. While there is an in-built function in python to reverse the list.

In [45]: prime_numbers = [2, 3, 5, 7]

In [46]: prime_numbers.reverse()

In [47]: prime_numbers
Out[47]: [7, 5, 3, 2]

In [48]: 'bowrna'.reverse()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-48-fa3d82d3e50e> in <module>
----> 1 'bowrna'.reverse()

AttributeError: 'str' object has no attribute 'reverse'

Here the reverse function actually reverses the elements in the list in place. It doesn't work for other data types.

The second way

There is another function reversed in Python that returns an iterator that will iterate over the object's element in the reverse order.

In [61]: prime_numbers
Out[61]: [7, 5, 3, 2]

In [62]: reversed(prime_numbers)
Out[62]: <list_reverseiterator at 0x1070e16a0>

In [63]: type(reversed(prime_numbers))
Out[63]: list_reverseiterator

In [64]: for t in reversed(prime_numbers):
    ...:     print(t)
    ...:
    ...:
2
3
5
7

In [67]: test_string='bowrna'

In [68]: for t in reversed(test_string):
    ...:     print(t)
    ...:
    ...:
a
n
r
w
o
b

In [69]: test=(1,2,3)

In [70]: for t in reversed(test):
    ...:     print(t)
    ...:
    ...:
    ...:
3
2
1

This was the way suggested by the interviewer. It works for different data types like string, list, and tuple. But I was trying to get the list reversed by using the slicing technique.

The third way

Slicing is the way to access part of the object like a string, list, tuple, or any iterable. You can access a specific element of any iterable using its index. While indexing is used to access a specific element and perform operations on it, slicing is used to access multiple elements using the index. Given a sequence, you can slice it in the following method.

sequence[start: stop: step]

Here start and stop indicate the index of the sequence to access and step indicates the increment between the start and stop in the sequence. You can refer to this article to know more about slicing. This will give you a better understanding of the slicing technique.

Using the Negative step in Slicing to reverse the iterable

Below is the way to reverse the list by giving the step as -1. Here the start and stop value is not specified as we have to reverse the entire list. This is the way that I tried to reverse the list in my interview process. While I was right in specifying the negative step for reversing the list, I got the syntax for writing this code wrong.

In [71]: test[::-1]
Out[71]: (3, 2, 1)

In [72]: test_string[::-1]
Out[72]: 'anrwob'

In [73]: prime_numbers[::-1]
Out[73]: [2, 3, 5, 7]

In [74]: test
Out[74]: (1, 2, 3)

In [75]: test_string
Out[75]: 'bowrna'

In [76]: prime_numbers
Out[76]: [7, 5, 3, 2]

In [77]: test[::-1]
Out[77]: (3, 2, 1)

In [78]: test_string[::-1]
Out[78]: 'anrwob'

In [79]: prime_numbers[::-1]
Out[79]: [2, 3, 5, 7]

Hope you find this post useful. If you have any feedback or comments to improve this article, please share it :) Thank you for reading.

Photo by Markus Spiske on Unsplash

Reference: codingem.com/reverse-slicing-in-python