Rolex Pearlmaster Replica
  Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
This article is part of in the series
Published: Wednesday 18th October 2017
Last Updated: Monday 31st July 2023

Prerequisites

To learn about in place list reversal, you should know the following:

  1. Python 3
  2. Python data structures - Lists
  3. Swapping values

What is in place list reversal?

In the previous posts, you might have learned how to read a list in reverse in Python which is quite fun. You can check this out here. However, the motive of this tutorial is quite different. We are not going to read a list in reverse but we are going to change an existing list to its reversed self. Note that this is a language agnostic approach.

Let’s look at an example:

alist = [1,2,3,4,5,6,7]

print(alist[::-1]) #prints [7,6,5,4,3,2,1]

print(alist) #prints [1,2,3,4,5,6,7]

So you are merely reading alist in the reverse order when you do alist[::-1]. What we want to achieve is to make alist = [7,6,5,4,3,2,1].

How to implement this?

The idea behind this is to use two pointers, left and right. The left pointer points to the first index of the list and the right pointer points to the last index of the list. Now we swap the elements pointed to by these pointers. Then, we move the pointer to the next indices. The terminating condition of this would be when the left pointer equals or crosses over the right pointer. Let us see this using an example:

Round alist left right alist after swapping
1 [1,2,3,4,5,6,7] 1 [0] 7 [6] [7,2,3,4,5,6,1]
2 [7,2,3,4,5,6,1] 2[1] 6[5] [7,6,3,4,5,2,1]
3 [7,2,3,4,5,6,1] 3[2] 5[4] [7,6,5,4,3,2,1]
4 [7,6,5,4,3,2,1] 4[3] 4[3] Stop since left == right


As you can see, now the list is reversed.

Algorithm

  1. left pointer points to the first index and right pointer points to the last index
  2. Swap elements pointed by the left and right pointers respectively
  3. Increment the left pointer by 1 and decrement the right pointer by 1
  4. Check if left>= right:
    1. If no, repeat steps 2-4
    2. If yes, stop. The list reversal is complete

Note: Although this algorithm is explained for lists, you can use this on strings as well. In that case, you might have to convert the string to a list by typecasting it. Do not forget to reconstruct the string back from the list format. This can be done by using some of the formatting commands like replace().

Code

def reverse(alist):

   #intializing pointers
   left = 0
   right = len(alist)-1

   #condition for termination
   while left<right:

       #swapping
       temp = alist[left]
       alist[left] = alist[right]
       alist[right] = temp

       #updating pointers
       left += 1
       right -= 1

   return alist

print(reverse([1,2,3,4,5]))

Efficiency

You might think that instead of going through all this trouble, you can create a new list with reversed contents. Something like this:

alist = [1,2,3,4,5,6,7]
blist = list()
for item in alist[::-1]:
blist.append(item)
print(blist) #prints [7,6,5,4,3,2,1]

Although the above approach works, you are using additional memory (blist) to store the reversed list. This might be a serious concern if the number of elements in the list is large. That is when the in place reversal will come in handy. The efficiency of in place reversal is O(n) since we visit all the elements of the list at least once.

Reversing a List In-Place with .reverse()

Python features a built-in method .reverse(), making in-place list reversals much easier. You can use a dot notation to call this method on a list. It reverses the list's elements and updates the list accordingly. So, this is the right method to use only if you aren't concerned about preserving the original order of your list.

Bear in mind that .reverse() does not accept any arguments. It also has no return value since it can only update an existing list. The syntax of this Python method is:

name_of_list.reverse()

Here is an example of how you can use the .reverse() method:

list_of_numbers = [1, 2, 3, 4, 5]

# Calling .reverse()
list_of_numbers.reverse()

# Printing the new list_of_numbers 
print(list_of_numbers)

# The output is: 
# [5, 4, 3, 2, 1]

You can also use this method on a list of names, like so:

list_of_names = ["Alice", "Bob", "Eve", "Mike"]

# Calling .reverse()
list_of_names.reverse()

# Printing the new list_of_names
print(list_of_names)

# output
# ['Mike', 'Eve', 'Bob', 'Alice']

Reversing a List In-Place Using reversed()

The reversed() function comes in handy when you need to access a list's elements individually in reverse order. 

Like .reverse(), the reversed() function also comes built-in with Python. But unlike .reverse(), the reversed() function does not modify the supplied list. It also does not create a new list. It only returns a reversed iterator. Here is reversed() in action:

list_of_numbers = [1, 2, 3, 4, 5]

reversed_list_of_numbers = reversed(list_of_numbers)

# printing original list
print(list_of_numbers)

# printing new "list"
print(reversed_list_of_numbers)

# checking the data type of reversed_list_of_numbers 
print(type(reversed_list_of_numbers))

# output
# [1, 2, 3, 4, 5]
# <list_reverseiterator object at 0x7fca9b4da380>
# <class 'list_reverseiterator'>

The second line in the output indicates that reversed_list_of_numbers is an object and not a list. Yet, Python's built-in list() function can be utilized to transform the iterator into a list. Here's what this looks like:

list_of_numbers = [1, 2, 3, 4, 5]

reversed_list_of_numbers = list(reversed(list_of_numbers))

# printing original list
print(list_of_numbers)

# printing new list
print(reversed_list_of_numbers)

# output
# [1, 2, 3, 4, 5]
# [5, 4, 3, 2, 1]

It's interesting to note that you can obtain a list's items in reverse order using a for loop conjunctly with the reversed() function. Here's an example:

list_of_numbers = [1, 2, 3, 4, 5]

for number in reversed(list_of_numbers):
  print(number)
 
# output
# 5
# 4
# 3
# 2
# 1

Conclusion

This is a very commonly asked interview question. Try using the same approach to solve these puzzles and comment below:

  1. Reverse the string “hello”. Output should be “olleh”
  2. Reverse the sentence “I love Python Central”. Output should be “Central Python love I”
  3. Check if “madam” is a palindrome or not

We will use this concept in future tutorials like linked list reversal. That’s it for this tutorial. Happy Pythoning!

Latest Articles


Tags

  • Unpickling
  • array
  • sorting
  • reversal
  • Python salaries
  • list sort
  • Pip
  • .groupby()
  • pyenv global
  • NumPy arrays
  • Modulo
  • OpenCV
  • Torrent
  • data
  • int function
  • file conversion
  • calculus
  • python typing
  • encryption
  • strings
  • big o calculator
  • gamin
  • HTML
  • list
  • insertion sort
  • in place reversal
  • learn python
  • String
  • python packages
  • FastAPI
  • argparse
  • zeros() function
  • AWS Lambda
  • Scikit Learn
  • Free
  • classes
  • turtle
  • convert file
  • abs()
  • python do while
  • set operations
  • data visualization
  • efficient coding
  • data analysis
  • HTML Parser
  • circular queue
  • effiiciency
  • Learning
  • windows
  • reverse
  • Python IDE
  • python maps
  • dataframes
  • Num Py Zeros
  • Python Lists
  • Fprintf
  • Version
  • immutable
  • python turtle
  • pandoc
  • semantic kernel
  • do while
  • set
  • tabulate
  • optimize code
  • object oriented
  • HTML Extraction
  • head
  • selection sort
  • Programming
  • install python on windows
  • reverse string
  • python Code Editors
  • Pytest
  • pandas.reset_index
  • NumPy
  • Infinite Numbers in Python
  • Python Readlines()
  • Trial
  • youtube
  • interactive
  • deep
  • kernel
  • while loop
  • union
  • tutorials
  • audio
  • github
  • Parsing
  • tail
  • merge sort
  • Programming language
  • remove python
  • concatenate string
  • Code Editors
  • unittest
  • reset_index()
  • Train Test Split
  • Local Testing Server
  • Python Input
  • Studio
  • excel
  • sgd
  • deeplearning
  • pandas
  • class python
  • intersection
  • logic
  • pydub
  • git
  • Scrapping
  • priority queue
  • quick sort
  • web development
  • uninstall python
  • python string
  • code interface
  • PyUnit
  • round numbers
  • train_test_split()
  • Flask module
  • Software
  • FL
  • llm
  • data science
  • testing
  • pathlib
  • oop
  • gui
  • visualization
  • audio edit
  • requests
  • stack
  • min heap
  • Linked List
  • machine learning
  • scripts
  • compare string
  • time delay
  • PythonZip
  • pandas dataframes
  • arange() method
  • SQLAlchemy
  • Activator
  • Music
  • AI
  • ML
  • import
  • file
  • jinja
  • pysimplegui
  • notebook
  • decouple
  • queue
  • heapify
  • Singly Linked List
  • intro
  • python scripts
  • learning python
  • python bugs
  • ZipFunction
  • plus equals
  • np.linspace
  • SQLAlchemy advance
  • Download
  • No
  • nlp
  • machiine learning
  • dask
  • file management
  • jinja2
  • ui
  • tdqm
  • configuration
  • deque
  • heap
  • Data Structure
  • howto
  • dict
  • csv in python
  • logging in python
  • Python Counter
  • python subprocess
  • numpy module
  • Python code generators
  • KMS
  • Office
  • modules
  • web scraping
  • scalable
  • pipx
  • templates
  • python not
  • pytesseract
  • env
  • push
  • search
  • Node
  • python tutorial
  • dictionary
  • csv file python
  • python logging
  • Counter class
  • Python assert
  • linspace
  • numbers_list
  • Tool
  • Key
  • automation
  • website data
  • autoscale
  • packages
  • snusbase
  • boolean
  • ocr
  • pyside6
  • pop
  • binary search
  • Insert Node
  • Python tips
  • python dictionary
  • Python's Built-in CSV Library
  • logging APIs
  • Constructing Counters
  • Assertions
  • Matplotlib Plotting
  • any() Function
  • Activation
  • Patch
  • threading
  • scrapy
  • game analysis
  • dependencies
  • security
  • not operation
  • pdf
  • build gui
  • dequeue
  • linear search
  • Add Node
  • Python tools
  • function
  • python update
  • logging module
  • Concatenate Data Frames
  • python comments
  • matplotlib
  • Recursion Limit
  • License
  • Pirated
  • square root
  • website extract python
  • steamspy
  • processing
  • cybersecurity
  • variable
  • image processing
  • incrementing
  • Data structures
  • algorithm
  • Print Node
  • installation
  • python function
  • pandas installation
  • Zen of Python
  • concatenation
  • Echo Client
  • Pygame
  • NumPy Pad()
  • Unlock
  • Bypass
  • pytorch
  • zipp
  • steam
  • multiprocessing
  • type hinting
  • global
  • argh
  • c vs python
  • Python
  • stacks
  • Sort
  • algorithms
  • install python
  • Scopes
  • how to install pandas
  • Philosophy of Programming
  • concat() function
  • Socket State
  • % Operator
  • Python YAML
  • Crack
  • Reddit
  • lightning
  • zip files
  • python reduce
  • library
  • dynamic
  • local
  • command line
  • define function
  • Pickle
  • enqueue
  • ascending
  • remove a node
  • Django
  • function scope
  • Tuple in Python
  • pandas groupby
  • pyenv
  • socket programming
  • Python Modulo
  • Dictionary Update()
  • Hack
  • sdk
  • python automation
  • main
  • reduce
  • typing
  • ord
  • print
  • network
  • matplotlib inline
  • Pickling
  • datastructure
  • bubble sort
  • find a node
  • Flask
  • calling function
  • tuple
  • GroupBy method
  • Pythonbrew
  • Np.Arange()
  • Modulo Operator
  • Python Or Operator
  • Keygen
  • cloud
  • pyautogui
  • python main
  • reduce function
  • type hints
  • python ord
  • format
  • python socket
  • jupyter
  • Python is a beautiful language.