CameraIcon
CameraIcon
SearchIcon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

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

Open in App
Solution

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]

flag
Suggest Corrections
thumbs-up
0
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Organisation of Data
MATHEMATICS
Watch in App
Join BYJU'S Learning Program
CrossIcon