Published in



Reversing Different Objects in Different Ways in Python

Photo by Sam Barber on Unsplash

Hello, We are going to explore various ways of reversing objects (string, list, …) in python.

Reversing a list with reverse() method

This method does not take any arguments and return anything. It just reverses the original list.

code 1

Reversing a list with reversed() function

It returns an iterator object so that we can iterate over the list in reverse order. It does not change the original list.

We can use either list comprehension (in the image) or list() function to create the reversed of the original list as a new list.

code 2

With list() function which uses iteration but internally, we should write x=list(r). We should pay attention that if we apply one of these two ways on r again, we get an empty list.

code 3

This is because r is an iterator so when doing above statements python internally behind the scene uses next(r) in each iteration to access values inside r. the following code makes my point.

With iter() function we can create an iterator from a list.

code 4

Reversing a list with reversed subsetting

This returns a copy of the list.

code 5

Reversing a list with NumPy library

NumPy is a python library for linear algebra. We can use it’s flip() method to reverse a list (array) created with NumPy. It returns a new array.

code 6

Reversing a string with reversed() function

code 7

Reversing a string with reversed subsetting

code 8

Reversing matrices along chosen axis with NumPy library

I demonstrate this on a 2D matrix. the flip() method about which I talked earlier has a second optional argument that can be None or integer or tuple of integers. use that to flip along desired axis or axes.

The default, axis=None, will flip over all of the axes of the input array. If axis is a tuple of integers, flipping is performed on all of the axes specified in the tuple.

code 9

If you are confused about it, along axis 0 means in vertical direction along the rows and considering the items of the most outer level of brackets. Along the axis 1 means in horizontal direction along the columns and considering the items of the second most outer level of brackets.

Please refer to this link at the documentation for a 3D example:

Reversing an integer

In this example if you follow each iteration of the loop you see that the process inside the loop is easy. (pen and paper can help)

code 10

Reversing a tuple with reversed subsetting

code 11

Reversing a tuple with reversed() function

code 12

Reversing each key-value pair in a dictionary

code 13

Thanks for reading. wish you peace. if you liked it please press clap option.




Everything connected with Tech & Code. Follow to join our 900K+ monthly readers

Recommended from Medium

There is NOT a shortage of Programmers

NestJS Interceptors

Where “MVP” journey started

2019–2020 Trends in Complex Website Design & Development for Small To Medium Sized Businesses

How I implemented Snake and Apple Game using PyGame in Python

SoccerHub’s next steps: What are they?

How to Stake $SCH in SoccerHub

When is the right time to do a code audit?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


I write on topics that help us have a better life both financially (DeFi, business,…) and mentally (art, philosophy,…)

More from Medium

Python Dictionaries Under The Hood

Mastering Python for Beginner — Part 3: Syntax Rules

Tkinter GUI Programming

How Do You Convert a String to an Integer and Vice Versa in Python?