Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
121 views
in Computer by (51.9k points)
closed by

Read a list of n elements. Pass this list to a function which reverses this list in-place without creating a new list.

1 Answer

+1 vote
by (49.5k points)
selected by
 
Best answer
Program:

def reverseList():

global list1

#Using reverse() function to reverse the list in place

#This will not create any new list

list1.reverse()

print("Reversed List:",list1)

#Defining empty list

list1 = list()

#Getting input for number of elements to be added in the list

inp = int(input("How many elements do you want to add in the list? "))

#Taking the input from user

for i in range(inp):

a = int(input("Enter the elements: "))

list1.append(a)

#Printing the list

print("The list entered is:",list1)

#The function reverseList is called to reverse the list

reverseList()
​
OUTPUT:

How many elements do you want to add in the list? 6

Enter the elements: 1

Enter the elements: 2

Enter the elements: 4

Enter the elements: 5

Enter the elements: 9

Enter the elements: 3

The list entered is: [1, 2, 4, 5, 9, 3]

Reversed List: [3, 9, 5, 4, 2, 1]

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...