Advertisements
Advertisements
प्रश्न
Read a list of n elements. Pass this list to a function that reverses this list in place without creating a new list.
संक्षेप में उत्तर
Advertisements
उत्तर
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]shaalaa.com
Lists Methods and Built-in Functions in Python
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
