Methods and Functions

Python reversed()

In python, we can retrieve the reverse of a number, a string, a list, and so on. The python reverse() function returns the sequence’s reversed order.

For example:

Input: str = I love coding in python

Output: str = python in coding love I 

The syntax for the same is:

reversed(seq)

python reverse

Source Code

reversed() Parameters

The reversed function in python takes one argument as the input. 

seq-this is the sequence that must be reversed. 

Return value from reversed()

After implementing the reversed() in python, the reversed order of the given sequence will be returned. 

Example 1: Using reversed() in the string, tuple, list, and range

We used the list() method to convert the iterators returned by reversed() to a list in our example.

Source Code

                    

# for string

seqstring11 = 'Python'

print(list(reversed(seq_string)))



# for tuple

seqtuple11 = ('P', 'y', 't', 'h', 'o', 'n')

print(list(reversed(seq_tuple)))



# for range

seq_range = range(5, 9)

print(list(reversed(seq_range)))



# for list

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

print(list(reversed(seq_list)))

Output

['n', 'o', 'h', 't', 'y', 'P']

['n', 'o', 'h', 't', 'y', 'P']

[8, 7, 6, 5]

[5, 3, 4, 2, 1]



Example 2: reversed() in custom objects


class Vowels:

    vowels11 = ['a', 'e', 'i', 'o', 'u']



    def __reversed__(self):

        return reversed(self.vowels11)



v = Vowels()

print(list(reversed(v)))

Output

['u', 'o', 'i', 'e', 'a']

 

Is there a reverse function in Python?

Yes, the reversed() function allows us to reverse the order of items in a sequence. It takes a sequence as input and outputs an iterator. 

For example:

The algorithm of the program is given below:

 

  1. Using the split() function of the string data type in Python, separate each word in a given string.
  2. Reverse the list of words in the list. After combining each word with a space using ” “, print the list’s words as a string using the join() in python.

Source Code:

                    

def rev_sentence(sentence): 

  

    # First, break the string into words. 

    words1 = sentence.split(' ') 

  

# We need to reverse the list 

# using the split function and join 

# them using the space function

    reverse_sentence = ' '.join(reversed(words1)) 

  

    #return reverse sentence from the joined string

  

if __name__ == "__main__": 

    input = 'geeks quiz practice code'

    print (rev_sentence(input))



Output:

code practice quiz geeks

What is the reversed word in Python?

Example

                    

#creating the string



strg1 = "I am an expert python programmer"



# splitting the string on space

words1 = strg1.split()



# reversing the words using reversed() function



words1 = list(reversed(words))



# joining the words and printing

print(" ".join(words1))



Output



programmer python an am I expert

How do you reverse a number in Python?

                    

number = int(input("Enter the integer number: "))  

  

# Initiate value to null  

revs_number = 0  

  

# Using the while loop, reverse the integer number. 

  

while (number > 0):  

    # Logic applied for the program 



    remainder = number % 10  

    revs_number = (revs_number *10) + remainder  

    number = number // 10  

  

# Display the result  

print("The reverse number is : {}".format(revs_number))  



Output:

Enter the integer number: 12345

The reverse number is: 54321

How do you reverse a list in Python 3?

In the Python programming language, we can reverse a list using three methods. The three methods are:

  • Reverse the list using slicing
  • Reverse the list using Reverse()
  • Reverse the list using Reversed

 

Reversing the given list with the help of the slicing:

We declared our variable studentNames11 and then constructed a slice function to reverse the list in our code. This is a popular method of slicing lists. However, it is inefficient because it duplicates the list, which consumes memory. In addition, compared to other methods of reversing lists, this function can be a little more advanced.

Example:

                    

studentNames11 = ["Hannah", "Imogen", "Lewis", "Peter", "Zayn", "Louis", "Harry"]

print(studentNames11[::-1])


Output:


["Harry", "Louis", "Zayn", "Peter", "Lewis", "Imogen", "Hannah"]

Example for reversing the program using the reverse()

The second method is to use the built-in reverse function (). To reverse the contents of a list object in-place, use the reverse() function. You will not be generating a new list as a result of this. Instead, we’ll make changes to the original list object.

Source Code:

                    

list1 = ['physics', 'Biology', 'chemistry', 'maths']

list1.reverse()

print ("list now : ", list1)


Output

list now : ['maths', 'chemistry', 'Biology', 'physics']

Example: For reversing list using the reversed function

                    

studentNames2 = ["Harry", "Ron", "Hermoine"]



for j in reversed(studentNames2):

print(j)



Output



"Hermoine"

"Ron"

"Harry"

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.