A Python Program to Reverse a List in Two Ways?
Code Explanation:
● Method 1 (Slicing):
lst[::-1] uses Python slicing to reverse the list quickly.
● Method 2 (Loop):
A loop runs from the end of the list to the start (range(len(lst)-1, -1, -1)) and appends elements to a new list.
● Function Definitions:
Two separate functions handle the reversing by slicing and loop method.
● Input List:
A sample list [10, 20, 30, 40, 50] is used for testing both methods.
● Function Call & Output:
Both functions are called and their reversed outputs are printed.
Program:
# Method 1: Using slicing
def reverse_list_slicing(lst):
return lst[::-1]
# Method 2: Using loop
def reverse_list_loop(lst):
reversed_list = []
for i in range(len(lst)-1, -1, -1):
reversed_list.append(lst[i])
return reversed_list
# Sample list
my_list = [10, 20, 30, 40, 50]
# Reversing using both methods
print("Reversed using slicing:", reverse_list_slicing(my_list))
print("Reversed using loop:", reverse_list_loop(my_list))
Trainings :
Data Science Training in Indore | Data Analytics Training in Indore | Python Training in Indore | Blockchain Training in Indore | React JS Training in Indore | Web Development Training in Indore | Full Stack Development Training in Indore |Free Courses and Resource :
Verbal Aptitude | Matplotlib Tutorials | Examples | Interview Questions | Pandas Tutorials | Projects | Interview Questions | OpenCV Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Dart | Python Data Structures and Algorithms | Rust Tutorials | Projects | Interview Questions | Aptitude Tests | HackerRank Python | Django | MongoDB Tutorials | Examples | Interview Questions | NumPy Practice Questions | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | React.js Tutorials | Verbal Ability Tutorial | MS Sql Server Tutorials | Examples | Interview Questions | Power BI Tutorials | Projects | Interview Questions | Example Dashboards | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial | HackerRank C++ Solutions | Tensor Flow | Pandas Practice Questions | Python Practice Questions | C++ Tutorials | Quantitative Ability Tutorial | Javascript | Node.js Tutorials | HackerRank SQL Solutions | HackerRank DSA Solutions | Verbal Aptitude 2 | HackerRank Java Solutions | HTML Tutorial | TCS NQT Mock Test Series | Reasoning Ability Tutorial | CodeChef Python Solutions | HackerRank C Program Solutions | Leetcode Python Solutions | SQL Practice Question | Matplotlib Practice Questions |Interview Questions :
Pandas Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Django | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions |Top Colleges in India :
Indian Institute of Technology Bombay | Jaypee University of Engineering and Technology - Guna |