List Manipulation in Python Class 11 Questions and Answers

Share with others

Teachers and Examiners (CBSESkillEduction) collaborated to create the List Manipulation in Python Class 11 Questions and Answers. All the important Information are taken from the NCERT Textbook Computer Science (083) class 11.

List Manipulation in Python Class 11 Questions and Answers

1. What will be the output of the following statements?

Answer:

a. Sorting a list

list1 = [12, 32, 65, 26, 80, 10]
list1.sort()
print("Sorted list:", list1)

Output:
[10, 12, 26, 32, 65, 80]

b. Using sorted() without modifying the original list

ist1 = [12, 32, 65, 26, 80, 10]
print("Sorted version:", sorted(list1))

Output:
[10, 12, 26, 32, 65, 80]

c. Reverse slicing with step -2 and combining slices

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("Reverse slicing with step -2:", list1[::-2]) 
print("Concatenation of slices:", list1[:3] + list1[3:])

Output:
[10, 8, 6, 4, 2]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

d. Accessing the last element of a list

list1 = [1, 2, 3, 4, 5]
print("Last element:", list1[len(list1) - 1])

Output:
5

2. Consider the following list myList. What will be the elements of myList after the following two operations:
myList = [10,20,30,40]

Answer:

myList = [10,20,30,40]
myList.append([50,60])
print(myList)

Output:
[10, 20, 30, 40, [50, 60]]
myList = [10,20,30,40]
myList.extend([80,90])
print(myList)

Output:
[10, 20, 30, 40, 80, 90]

3. What will be the output of the following code segment:

Answer:

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])

Output:
1
3
5
7
9

4. What will be the output of the following code segment:

Answer:

a. Deleting elements from index 3 onwards

myList = [1,2,3,4,5,6,7,8,9,10]
del myList[3:]
print(myList)

Output:
[1, 2, 3]

b. Deleting the first five elements

myList = [1,2,3,4,5,6,7,8,9,10]
del myList[:5]
print(myList)

Output:
[6, 7, 8, 9, 10]

c. Deleting every second element

myList = [1,2,3,4,5,6,7,8,9,10]
del myList[::2]
print(myList)

Output:
[2, 4, 6, 8, 10]

5. Differentiate between append() and extend() functions of list.

Answer – append() function helps to adds a single element to the end of the list, extend() function can add multiple elements to the end of list.

6. Consider a list:
list1 = [6,7,8,9]

What is the difference between the following operations on list1:
a. list1 * 2
b. list1 *= 2
c. list1 = list1 * 2

Answer –

  1. The first line of the statement prints each element of the list twice, as in [6, 7, 8, 9, 6, 7, 8, 9]. List1, however, won’t be changed.
  2. List1 will be modified and the list with repeated elements, [6, 7, 8, 9, 6, 7, 8, 9], will be assigned to List1.
  3. This assertion will similarly have the same outcome as the assertion “list1 *= 2.” List1 will be given the list with the repeated elements, [6, 7, 8, 9, 6, 7, 8, 9].

7. The record of a student (Name, Roll No., Marks in five subjects and percentage of marks) is stored in the following list:

stRecord = ['Raman','A-36',[56,98,99,72,69],78.8]

Write Python statements to retrieve the following information from the list stRecord.

Answer:

a) Percentage of the student

stRecord[3]

b) Marks in the fifth subject

stRecord[2][4]

c) Maximum marks of the student

max(stRecord[2])

d) Roll no. of the student

stRecord[1]

e) Change the name of the student from ‘Raman’ to ‘Raghav’

stRecord[0] = "Raghav"

1. Write a program to find the number of times an element occurs in the list.

Answer –

list1 = [10, 20, 30, 40, 50, 60, 20, 50]

print("List is:", list1)

element = int(input("Enter the element you want to count: "))

count = list1.count(element)

print(f"Count of element {element} in the list is: {count}")

Output:
List is: [10, 20, 30, 40, 50, 60, 20, 50]
Enter the element you want to count: 20
Count of element 20 in the list is: 2

2. Write a program to read a list of n integers (positive as well as negative). Create two new lists, one having all positive numbers and the other having all negative numbers from the given list. Print all three lists.

Answer –

n = int(input("Enter number of terms: ")) 
arr = [] 
positive = [] 
negative = [] 

for i in range(n): 
    num = int(input()) 
    arr.append(num) 

for i in range(n): 
    if arr[i] >= 0: 
        positive.append(arr[i]) 
    else: 
        negative.append(arr[i]) 

print("Total positive numbers =", positive) 
print("Total negative numbers =", negative) 
print("All numbers =", arr)

Output:
Enter number of terms: 5
6
5
8
4
1
Total positive numbers = [6, 5, 8, 4, 1]
Total negative numbers = []
All numbers = [6, 5, 8, 4, 1]

3. Write a function that returns the largest element of the list passed as parameter.

Answer –

def find_max(arr):
    max_element = arr[0]
    for num in arr:
        if num > max_element:
            max_element = num
    return max_element

arr = [32, 92, 72, 36, 48, 105]

print("Largest element in the list:", find_max(arr))

Output:
Largest element in the list: 105

4. Write a function to return the second largest number from a list of numbers.

Answer –

def second_largest(new_list):
    return sorted(new_list)[-2]

new_list = [36, 95, 45, 90, 105]

print("Second largest number:", second_largest(new_list))

Output:
Second largest number: 95

5. 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 an built-in function to sort the list

Answer –

def find_median(numbers):
    numbers.sort()
    n = len(numbers)
    
    if n % 2 == 0:
        median = (numbers[n // 2 - 1] + numbers[n // 2]) / 2
    else:
        median = numbers[n // 2]
    
    return median

numbers = []
n = int(input("Enter number of terms: "))
for _ in range(n):
    num = int(input("Enter a number: "))
    numbers.append(num)

print("The median value is:", find_median(numbers))

Output:
Enter number of terms: 5
Enter a number: 6
Enter a number: 5
Enter a number: 8
Enter a number: 4
Enter a number: 6
The median value is: 6

6. Write a program to read a list of elements. Modify this list so that it does not contain any duplicate elements, i.e., all elements occurring multiple times in the list should appear only once.

Answer –

def remove_duplicates(lst):
    return list(set(lst))

list1 = []

inp = int(input("Enter number of terms: "))

for i in range(inp):
    num = int(input("Enter the number: "))
    list1.append(num)

print("The list is:", list1)
print("List without any duplicate elements:", remove_duplicates(list1))

Output:
Enter number of terms: 5
Enter the number: 6
Enter the number: 4
Enter the number: 8
Enter the number: 1
Enter the number: 6
The list is: [6, 4, 8, 1, 6]
List without any duplicate elements: [8, 1, 4, 6]

7. 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.

Answer –

def remove_duplicates(lst):
    return list(set(lst))

list1 = []

inp = int(input("Enter number of terms: "))

for i in range(inp):
    num = int(input("Enter the number: "))
    list1.append(num)

print("The list is:", list1)
print("List without any duplicate elements:", remove_duplicates(list1))

Output:
Enter number of terms: 5
Enter the number: 6
Enter the number: 4
Enter the number: 8
Enter the number: 1
Enter the number: 6
The list is: [6, 4, 8, 1, 6]
List without any duplicate elements: [8, 1, 4, 6]

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

Answer –

def reverse_list(lst):
    lst.reverse()

new_list = [98, 78, 54, 89, 76]

print("Original list:", new_list)

reverse_list(new_list)

print("List after reversing:", new_list)

Output:
Original list: [98, 78, 54, 89, 76]
List after reversing: [76, 89, 54, 78, 98]

Computer Science Class 11 Questions and Answers

Disclaimer: We have taken an effort to provide you with the accurate handout of “List Manipulation in Python Class 11 Questions and Answers“. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above CBSE study material present on our websites is for education purpose, not our copyrights. All the above content and Screenshot are taken from Computer Science Class 11 NCERT Textbook and CBSE Support Material which is present in CBSEACADEMIC website, This Textbook and Support Material are legally copyright by Central Board of Secondary Education. We are only providing a medium and helping the students to improve the performances in the examination. 

Images and content shown above are the property of individual organizations and are used here for reference purposes only.

For more information, refer to the official CBSE textbooks available at cbseacademic.nic.in


Share with others

Leave a Comment