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.
उत्तर
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]
APPEARS IN
संबंधित प्रश्न
Consider the following list myList. What will be the elements of myList after the following operation:
myList = [10,20,30,40]
myList.append([50,60])
Consider the following list myList. What will be the elements of myList after the following operation:
myList = [10,20,30,40]
myList.extend([80,90])
What will be the output of the following code segment:
myList = [1,2,3,4,5,6,7,8,9,10]
for i in range(0,len(myList)):
if i%2 == 0:
print(myList[i])
Differentiate between append() and extend() functions of list.
Write a function that returns the largest element of the list passed as a parameter.
Write a function to return the second largest number from a list of numbers.
Write a program to read a list of n integers and find their median.
Note: The median value of a list of values is the middle one when they are arranged in order. If there are two middle values then take their average.
Hint: You can use a built-in function to sort the list.
Write a program to read a list of elements. Input an element from the user that has to be inserted in the list. Also, input the position at which it is to be inserted. Write a user-defined function to insert the element at the desired position in the list.