February 3, 2022
Programming Empire
The following code shows an example to Reverse List Elements in Python. To begin with, create a list with initial values. Further, you can display list elements using a for loop. After that create a for loop. The len() function finds the total number of elements in the list. Hence, iterate through the loop for all elements in the list. In order to reverse the list, swap the first element with the last one. Likewise, swap the second element with the second last element. Once you reach in the middle of the list exit from the loop. In this manner, the list is reversed.
mylist=[7, 12, 90, 11, 55, 84, 5, 0, 377, 121, 69, 39, 29]
sum=0
print('list elements: ')
for i in range(len(mylist)):
print(mylist[i])
print('Reversing the list...')
for i in range(len(mylist)):
t=mylist[i]
mylist[i]=mylist[len(mylist)-(i+1)]
mylist[len(mylist)-(i+1)]=t
x=(len(mylist)-1)/2
if i==x:
break
print('list elements: ')
for i in range(len(mylist)):
print(mylist[i])
Output
Further Reading
How to Implement Inheritance in Python
Find Prime Numbers in Given Range in Python
Running Instructions in an Interactive Interpreter in Python
Deep Learning Practice Exercise
Deep Learning Methods for Object Detection
Image Contrast Enhancement using Histogram Equalization
Transfer Learning and its Applications
Examples of OpenCV Library in Python
Understanding Blockchain Concepts
Example of Multi-layer Perceptron Classifier in Python
Measuring Performance of Classification using Confusion Matrix
Artificial Neural Network (ANN) Model using Scikit-Learn
Popular Machine Learning Algorithms for Prediction
Long Short Term Memory – An Artificial Recurrent Neural Network Architecture
Python Project Ideas for Undergraduate Students
Creating Basic Charts using Plotly
Visualizing Regression Models with lmplot() and residplot() in Seaborn
Data Visualization with Pandas