I got a assignment in python programming language. That I need to reverse a given list in python programming. So how can I reverse the list elements in the reverse order?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people's questions, and connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
kiran kumar85
Yes below is the example program for to reverse list elements in python programming.
Like any other programming language, in python also we use as a function called reverse() the elements in the list.
Here is the program to reverse the list:
# Reversing Lists in Place
# Like other mutable sequence types, Python lists implement.reverse().#
This method reverses the underlying list in place for memory efficiency when you’re reversing
digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ; digits.reverse(); print(digits)
digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(len (digits) // 2):
digits[i], digits[-1 – i] = digits[-1 – i], digits[i]
print(digits)
# Creating Reversed Lists
digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
reversed_digits = reversed(digits);
print(list(reversed_digits))p
printlist(reversed (“I am Enjoying Python”)))
Output of the program:
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[‘n’, ‘o’, ‘h’, ‘t’, ‘y’, ‘P’,”, ‘g’, ‘n’, ‘i’, ‘y’, ‘o’, ‘j’, ‘n’, ‘E’,”, ‘m’, ‘a’, ”, ‘I’]