3 Ways To Reverse a String/List/Number in Python

The most frequent interview question asked in interviews is how do you reverse a string/list/number! There are many techniques and approaches to reverse a string or list in python 3+. We will discuss about each of these methods in this post. Starting with the traditional approach of using loops such as for/ while loop to inbuilt functions available in Python 3+.

Method 1: Reverse a String/List/Number Using Loop Method

Using method 1, we can reverse values using loops method. Loops iterate over a sequence (either a list, a tuple, a dictionary, a set, or a string) until end of the sequence.

#Reverse a String using for loop
def revstr(string):
    rev = ""
    for i in string:
        rev = i + rev
    return rev
print(revstr("DSFOR"))

# Reverse a List using while loop

def revlist(lists):
    n = len(lists)-1
    newlist = []
    while n >= 0:
        newlist.append(lists[n])
        n -= 1
    return newlist
print(revlist(["python","is","cool"]))

#Reverse a Number using while loop (Maths way)

def revnum(number):
    rev = 0
    while number > 0:
        reminder = number%10
        rev = rev*10 + reminder
        number = number // 10
    return rev
print(revnum(12345))

ROFSD
[‘cool’, ‘is’, ‘python’]
54321

Method 2: Reverse a List / String / Number using Slicing Technique in Python.

In Method 2, we use the slicing method to reverse the string. Using the Slice technique in python is one of the fastest methods to deal with time complexity.

# slicing string in reverse order
def revstr(string):
    string = string[::-1]
    return string

print(revstr("DSFOR"))

# slicing list in reverse order
def revlist(lists):
    lists = lists[::-1]
    return lists

print(revlist(["python","is","cool"]))

# slicing number in reverse order
def revnum(number):
    number = str(number)[::-1]
    return number

print(revnum(12345))

ROFSD
[‘cool’, ‘is’, ‘python’]
54321

Method 3: Using Python In-built library functions to reverse text or integer values

Method 3: In Method 3, we will discuss how to reverse string, list or number using inbuilt python functions. Using inbuilt functions, it saves lots of time in development. To deal with integers or numbers first, convert the numbers to string or list and then convert back to int again. Integers are not iterable in nature as it holds only one value.

# reverse string using inbuilt list function
def revstr(string):
    string = "".join(reversed(string))
    return string

print(revstr("DSFOR"))

# reverse list using inbuilt list function
def revlist(lists):
    lists.reverse()
    return lists

print(revlist([11,4,12,8,3,6]))

# reverse number using inbuilt list function
def revnum(number):
    number = "".join(reversed(str(number)))
    return number

print(revnum(123456))

ROFSD
[6, 3, 8, 12, 4, 11]
654321

The text or number manipulation can also be done using other inbuilt libraries or stack concepts. However, when dealing with such problem statements we should make sure that the time complexity should be less. Among all the 3 methods discussed above, the time complexity for the slicing method is quickest and fastest.

Read More: Data Cleaning Methods for NLP – Python

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply