Python reversed() Function with Examples

In the previous article, we have discussed Python Program for chr() Function
reversed() Function in Python:

The reversed() function returns an iterator object that has been reversed.

Syntax:

reversed(sequence)

Parameters

sequence: This is required.Any object that is iterable. (list,tuple..etc)

A sequence is an object that implements the sequence protocols __len__() and __getitem__(). For instance, tuple, string, list, range, and so on.

In addition, we can use reversed() in any object that implements __reverse__ ().

Return Value:

The reversed() function returns an iterator that iterates through the given sequence in reverse order.

Examples:

Example1:

Input:

Given string = 'Python-programs'
Given tuple = ("h", "e", "l", "l", "o")

Output:

['s', 'm', 'a', 'r', 'g', 'o', 'r', 'p', '-', 'n', 'o', 'h', 't', 'y', 'P']
['o', 'l', 'l', 'e', 'h']

Note: After applying the reverse function the result is converted into the list.Hence the result is in the list form.

Example2:

Input:

Given list =  [6, 7, 8, 9, 10]
Given range = (2, 12)

Output:

[10, 9, 8, 7, 6]
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2]

Program for reversed() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Get the reverse of a string by applying the reversed() function to the given string and convert it into a list using the list() function.
  • Print the result.
  • Give the tuple as static input and store it in another variable.
  • Get the reverse of a tuple by applying the reversed() function to the given tuple and convert it into a list using the list() function.
  • Print the result.
  • Give the list as static input and store it in another variable.
  • Get the reverse of a list by applying the reversed() function to the given list and convert it into a list using the list() function.
  • Print the result.
  • Give the lower and upper limits as arguments to the range() function and store it in a variable.
  • Get the reverse order of the range by applying the reversed() function to the given range and convert it into a list using the list() function.
  • Print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = 'Python-programs'
# Get the reverse of a string by applying the reversed() function to the
# given string and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_str)))
# Give the tuple as static input and store it in another variable.
gvn_tupl = ("h", "e", "l", "l", "o")
# Get the reverse of a tuple by applying the reversed() function to the
# given tuple and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_tupl)))
# Give the list as static input and store it in another variable.
gvn_lst = [6, 7, 8, 9, 10]
# Get the reverse of a list by applying the reversed() function to the
# given list and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_lst)))
# Give the lower and upper limits as arguments to the range()function and
# store it in a variable.
gvn_rangee = range(2, 12)
# Get the reverse order of the range by applying the reversed() function
# to the given range and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_rangee)))

Output:

['s', 'm', 'a', 'r', 'g', 'o', 'r', 'p', '-', 'n', 'o', 'h', 't', 'y', 'P']
['o', 'l', 'l', 'e', 'h']
[10, 9, 8, 7, 6]
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2]

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Get the reverse of a string by applying the reversed() function to the given string and convert it into a list using the list() function.
  • Print the result.
  • Give the tuple user input using list(),map(),input(),and split() functions and store it in another variable.
  • Get the reverse of a tuple by applying the reversed() function to the given tuple and convert it into a list using the list() function.
  • Print the result.
  • Give the list as user input using list(),map(),input(),and split() functions and store it in a variable.
  • Get the reverse of a list by applying the reversed() function to the given list and convert it into a list using the list() function.
  • Print the result.
  • Give the lower and upper limits as user input using the int(input()) function and store them in two separate variables.
  • Pass the given lower and upper as arguments to the range() function and store it in a variable.
  • Get the reverse order of the range by applying the reversed() function to the given range and convert it into a list using the list() function.
  • Print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Get the reverse of a string by applying the reversed() function to the
# given string and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_str)))
# Give the tuple user input using list(),map(),input(),and split() functions and
# store it in another variable.
gvn_tupl=tuple(map(int, input(
    'Enter some random Tuple Elements separated by spaces = ').split()))
# Get the reverse of a tuple by applying the reversed() function to the
# given tuple and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_tupl)))
# Give the list as user input using list(),map(),input(),and split() functions and
# store it in a variable.
gvn_lst=list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Get the reverse of a list by applying the reversed() function to the
# given list and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_lst)))
# Give the lower and upper limits as user input using the int(input()) function and
# store them in two separate variables.
gvn_lowrlmt=int(input("Enter some random number = "))
gvn_upprlmt=int(input("Enter some random number = "))
# Pass the given lower and upper as arguments to the range() function and store it in a variable.
gvn_rangee=range(gvn_lowrlmt, gvn_upprlmt)
# Get the numbers in reverse order by applying the reversed() function to the
# given range and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_rangee)))

Output:

Enter some random string = good morning
['g', 'n', 'i', 'n', 'r', 'o', 'm', ' ', 'd', 'o', 'o', 'g']
Enter some random Tuple Elements separated by spaces = 0 4 5 1 2
[2, 1, 5, 4, 0]
Enter some random List Elements separated by spaces = 7 4 2 6
[6, 2, 4, 7]
Enter some random number = 15
Enter some random number = 22
[21, 20, 19, 18, 17, 16, 15]

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.