Class | Subject | Book | Chapter | Session | Questions covered |
|---|---|---|---|---|---|
XI | Computer Science | Computer Science (NCERT) | 9 – Lists | 2026-27 | 16 of 16 |
Chapter at a glance
Idea | Key point |
|---|---|
List | Ordered, mutable sequence of elements of any type, in [ ] |
Indexing and slicing | Same as strings; nested lists use L[i][j] |
Operators | + concatenation, * repetition, in / not in, slicing |
Adding | append(x) adds one element; extend(seq) adds each element of seq; insert(i, x) |
Removing | remove(x) first occurrence; pop(i) by index (returns it); del L[i] / del L[a:b] |
Others | count(), index(), reverse(), sort() (in place), sorted() (new list), len(), min(), max(), sum() |
Copying | L2 = L1 makes an alias; L1[:], list(L1) or L1.copy() make a copy |
Lists and functions | A list passed to a function can be changed in place |
Exercise Questions
What will be the output of the following statements?
i. list1 = [12,32,65,26,80,10]; list1.sort(); print(list1)
ii. list1 = [12,32,65,26,80,10]; sorted(list1); print(list1)
iii. list1 = [1,2,3,4,5,6,7,8,9,10]; list1[::-2]; list1[:3] + list1[3:]
iv. list1 = [1,2,3,4,5]; list1[len(list1)-1]
Answer
Answer
- sort() sorts the list in place → [10, 12, 26, 32, 65, 80]
- sorted() returns a new sorted list, which is not stored, so list1 is unchanged → [12, 32, 65, 26, 80, 10]
- No print() is used, so a script shows nothing; in the interactive shell the values are displayed: [10, 8, 6, 4, 2] and [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- Last element → 5 (displayed in the interactive shell)
- [10, 12, 26, 32, 65, 80] ii) [12, 32, 65, 26, 80, 10] iii) [10, 8, 6, 4, 2] and [1, 2, …, 10] (in interactive mode) iv) 5
Consider the following list myList. What will be the elements of myList after the following two operations: myList = [10,20,30,40]
i. myList.append([50,60]) ii. myList.extend([80,90])
Answer
Answer
- append() adds the whole list as one element → [10, 20, 30, 40, [50, 60]]
- extend() adds each element separately → [10, 20, 30, 40, [50, 60], 80, 90]
After i: [10, 20, 30, 40, [50, 60]]; after ii: [10, 20, 30, 40, [50, 60], 80, 90]
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])
Answer
Elements at even indices 0, 2, 4, 6, 8 are printed, each on a new line:
1
3
5
7
9
1, 3, 5, 7, 9 (one per line)
What will be the output of the following code segment:
a. myList = [1,...,10]; del myList[3:]; print(myList)
b. myList = [1,...,10]; del myList[:5]; print(myList)
c. myList = [1,...,10]; del myList[::2]; print(myList)
Answer
Answer
- Deletes from index 3 onwards → [1, 2, 3]
- Deletes the first five elements → [6, 7, 8, 9, 10]
- Deletes elements at indices 0, 2, 4, 6, 8 → [2, 4, 6, 8, 10]
- [1, 2, 3] b) [6, 7, 8, 9, 10] c) [2, 4, 6, 8, 10]
Differentiate between append() and extend() functions of list.
Answer
Answer
append() | extend() |
|---|---|
Adds a single element (of any type) at the end of the list | Adds each element of a sequence (list, tuple, string) at the end |
Length increases by 1 | Length increases by the number of elements added |
L = [1, 2]; L.append([3, 4]) → [1, 2, [3, 4]] | L = [1, 2]; L.extend([3, 4]) → [1, 2, 3, 4] |
append() adds its argument as one element (a list becomes a nested list); extend() adds the individual elements of the sequence.
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
Answer
- Creates a new list [6, 7, 8, 9, 6, 7, 8, 9] but does not store it; list1 stays [6, 7, 8, 9].
- Repeats the list in place — the same list object is modified to [6, 7, 8, 9, 6, 7, 8, 9]. Any other variable referring to the same list also sees the change.
- Creates a new list [6, 7, 8, 9, 6, 7, 8, 9] and makes list1 refer to it; the original list object is not changed (other aliases still hold [6, 7, 8, 9]).
- makes a new repeated list without changing list1; b) modifies list1 in place; c) creates a new repeated list and rebinds list1 to it.
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.
a) Percentage of the student b) Marks in the fifth subject c) Maximum marks of the student d) Roll no. of the student e) Change the name of the student from ‘Raman’ to ‘Raghav’
Answer
Answer
print(stRecord[3]) # a) 78.8 (or stRecord[-1])
print(stRecord[2][4]) # b) 69
print(max(stRecord[2])) # c) 99
print(stRecord[1]) # d) A-36
stRecord[0] = 'Raghav' # e)
- stRecord[3] b) stRecord[2][4] c) max(stRecord[2]) d) stRecord[1] e) stRecord[0] = ‘Raghav’
Question P1
Write a program to find the number of times an element occurs in the list.
Answer
L = eval(input("Enter a list, e.g. [1, 2, 2, 3]: "))
x = eval(input("Enter element to count: "))
count = 0
for item in L:
if item == x:
count += 1
print(x, "occurs", count, "times") # same as L.count(x)
Sample run: [4, 7, 4, 9, 4], 4 → 4 occurs 3 times
Traverse the list incrementing a counter whenever item == x (or use L.count(x)).
Question P2
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("How many integers? "))
L = []
for i in range(n):
L.append(int(input("Enter integer: ")))
pos, neg = [], []
for x in L:
if x > 0:
pos.append(x)
elif x < 0:
neg.append(x)
print("Original:", L)
print("Positive:", pos)
print("Negative:", neg)
Sample run: [5, −3, 8, 0, −1] → Positive [5, 8], Negative [−3, −1] (0 is neither).
Read n integers into L, append x > 0 to pos and x < 0 to neg, and print all three lists.
Question P3
Write a function that returns the largest element of the list passed as parameter.
Answer
def largest(L):
big = L[0]
for x in L[1:]:
if x > big:
big = x
return big
print(largest([23, 7, 56, 12, 41])) # 56
Assume L[0] is largest, compare with every other element and update; return it (same as max(L)).
Question P4
Write a function to return the second largest number from a list of numbers.
Answer
def second_largest(L):
first = second = None
for x in L:
if first is None or x > first:
second = first
first = x
elif x != first and (second is None or x > second):
second = x
return second
print(second_largest([23, 7, 56, 12, 56, 41])) # 41
Duplicates of the largest value are skipped; the function returns None if there is no second distinct value.
Track the largest and second-largest distinct values in one pass (or use sorted(set(L))[-2]).
Question P5
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.
Answer
n = int(input("How many integers? "))
L = [int(input("Enter integer: ")) for i in range(n)]
L.sort()
mid = n // 2
if n % 2 == 1:
median = L[mid]
else:
median = (L[mid - 1] + L[mid]) / 2
print("Median =", median)
Sample runs: [7, 1, 5] → sorted [1, 5, 7] → median 5; [8, 2, 6, 4] → sorted [2, 4, 6, 8] → (4 + 6)/2 = 5.0
Sort the list; median = middle element if n is odd, otherwise the average of the two middle elements.
Question P6
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
L = eval(input("Enter a list: "))
i = 0
while i < len(L):
if L[i] in L[:i]: # already seen earlier
L.pop(i)
else:
i += 1
print("List without duplicates:", L)
Sample run: [1, 2, 2, 3, 1, 4] → [1, 2, 3, 4] (original order is kept, and the same list is modified).
Traverse the list and remove (pop) any element already present earlier in it, keeping the first occurrence.
Question P7
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 insert_at(L, pos, x):
L.append(None) # make room
for i in range(len(L) - 1, pos, -1):
L[i] = L[i - 1] # shift right
L[pos] = x
L = eval(input("Enter a list: "))
x = eval(input("Element to insert: "))
pos = int(input("Position (index): "))
insert_at(L, pos, x) # same as L.insert(pos, x)
print(L)
Sample run: [10, 20, 40], 30, 2 → [10, 20, 30, 40]
Shift elements from the end one place right up to pos and place x at L[pos] (equivalent to L.insert(pos, x)).
Question P8
Write a program to read elements of a list.
a) The program should ask for the position of the element to be deleted from the list. Write a function to delete the element at the desired position in the list.
b) The program should ask for the value of the element to be deleted from the list. Write a function to delete the element of this value from the list.
Answer
def delete_at(L, pos):
if 0 <= pos < len(L):
return L.pop(pos)
print("Invalid position")
def delete_value(L, val):
if val in L:
L.remove(val) # removes the first occurrence
else:
print(val, "not found")
L = eval(input("Enter a list: "))
pos = int(input("Position to delete: "))
delete_at(L, pos)
print("After deleting by position:", L)
val = eval(input("Value to delete: "))
delete_value(L, val)
print("After deleting by value:", L)
Sample run: [5, 10, 15, 20], position 1 → [5, 15, 20]; value 20 → [5, 15]
- Use L.pop(pos) after checking the position is valid; b) use L.remove(val) after checking val in L.
Question P9
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_in_place(L):
i, j = 0, len(L) - 1
while i < j:
L[i], L[j] = L[j], L[i] # swap ends
i += 1
j -= 1
n = int(input("How many elements? "))
L = [eval(input("Enter element: ")) for k in range(n)]
reverse_in_place(L)
print("Reversed list:", L)
Sample run: [1, 2, 3, 4, 5] → [5, 4, 3, 2, 1]. Because lists are mutable, the change is visible to the caller.
Swap L[i] and L[j] moving i forward and j backward until they meet — the list is reversed in place.
Topic checklist
- ☒ List operations, sort vs sorted, slicing and del (Q1, 3, 4, 6)
- ☒ append vs extend (Q2, 5)
- ☒ Nested lists (Q7)
- ☒ List programs and functions (P1–P9)