Reverse Python list using reverse() function:
The reverse() function permanently reverses the python list’s content without creating a new list. It does not contain any argument. Create a python file with the following script to check the way reversing the items of the python list permanently by using the reverse() function. A list of 6 elements has been declared in the script. The original and reversed lists will be printed after executing the script.
languages = ['Python', 'PHP', 'Perl', 'Bash', 'Java', 'C++']
# Print the original list
print('The original list values:', languages)
# Reverse list
languages.reverse()
# Print the reversed list
print('The Reversed list values using reverse():', languages)
Output:
The following output will appear after executing the above script.
Reverse Python list using reversed() function:
Using the reversed() function is another way to traverse the python list in reverse order. The reversed() function does not modify the content of the python list. It returns an iterator to read the list values of the list in reverse order, and the content of the original list remains unchanged. Create a python file with the following script to check the use of the reversed() function for reading the list in reverse order. The first ‘for’ loop has been used to read the original content of the list, and the second for loop has been used with reversed() function to read the list in reverse order with the tab space.
languages = ['Python', 'PHP', 'Perl', 'Bash', 'Java', 'C++']
# Print the original list
print('The original list values:')
for value in languages:
print(value, end="\t")
# Print the reversed list
print('\nThe reversed list values using reversed():')
for value in reversed(languages):
print(value, end="\t")
Output:
The following output will appear after executing the above script.
Reverse Python list using range(n, -1, -1) function:
Create a python file with the following script to read the python list in reverse order by using the range() and len() functions. The len() function has been used in the script to read the last index of the list, and the range() function has been used to traverse the list from the last index to the first index. Like the previous example, the first ‘for’ loop has been used to read the original content of the list, and the second for loop has been used to read the list in reverse order with the tab space.
languages = ['Python', 'PHP', 'Perl', 'Bash', 'Java', 'C++']
# Print the original list
print('The original list values:')
for value in languages:
print(value, end="\t")
# Print the reversed list
print('\nThe reversed list values using range() and len():')
for index in range(len(languages) - 1, -1, -1) :
print(languages[index], end="\t")
Output:
The following output will appear after executing the above script.
Reverse the string using slicing:
The values of the python list can be traversed in reverse order without using any function. Create a python file with the following script to read the list values in reverse order by slicing the list. In this case, the starting position of the list index has been set to the last index by using [::-1], and the value of the list will be iterated backward. Like the previous example, the first ‘for’ loop has been used to read the original content of the list, and the second for loop has been used to read the list in reverse order with the tab space.
languages = ['Python', 'PHP', 'Perl', 'Bash', 'Java', 'C++']
# Print the original list
print('The original list values:')
for value in languages:
print(value, end="\t")
# Print the reversed list
print('\nThe reversed list values using slicing:')
for value in languages[::-1]:
print(value, end="\t")
Output:
The following output will appear after executing the above script.
Reverse list using the loop:
Create a python file with the following script to traverse the content of the python list in reverse order. It is the simplest way to reverse the list. The for loop has been used here to read and print the original values of the list. The while loop has been used to traverse the list backward. The index variable has been used to set the last index of the list, and it has been used in the loop to set the termination condition. The while loop will be iterated until the index value becomes 0. The original and the reversed values of the list will be printed with the tab space.
languages = ['Python', 'PHP', 'Perl', 'Bash', 'Java', 'C++']
# Print the original list
print('The original list values:')
for value in languages:
print(value, end="\t")
# Get the last index
index = len(languages) - 1
# Print the reversed list
print('\nThe reversed list values using loop:')
while index >= 0:
print(languages[index], end="\t")
index = index - 1
Output:
The following output will appear after executing the above script.
Conclusion:
Sometimes it requires reading the python list in reverse order for programming purposes. Traverse the list in reverse order by using the built-in functions, slicing, and the loop has been shown in this tutorial by using multiple examples to help the new python users.