Lists — NCERT Class 11 Computer Science Exercise Solutions
How to Use This Page
Read each question carefully and try to solve it on your own before looking at the answer. Compare your approach with the solution provided. Pay attention to the code comments — they explain the key steps. If your output differs, trace through the code line by line to find where you diverged. Practice typing out the code in your Python IDE rather than copying it; muscle memory helps in exams.
Key Concepts Recap
Before attempting the exercises, make sure you are comfortable with these concepts:
- Creating lists with
[]orlist() - Accessing elements by positive and negative index
- Slicing with
list[start:stop:step] - List methods:
append(),extend(),insert(),remove(),pop(),sort(),reverse(),count(),index(),clear(),copy() - List operations:
+(concatenation),*(repetition),in/not in(membership) - List comprehension syntax:
[expr for item in iterable if condition] - Nested lists and how to access
matrix[i][j] - Difference between
sort()(in place, returnsNone) andsorted()(returns new list) - Shallow copy vs deep copy for nested lists
Exercise Questions and Answers
Q1. Create a list of the first 10 even numbers and print the third and seventh elements.
# Create list of first 10 even numbers
evens = list(range(0, 20, 2))
print("List:", evens) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
print("Third element (index 2):", evens[2]) # 4
print("Seventh element (index 6):", evens[6]) # 12
Output:
Q2. Given the list fruits = ["apple", "banana", "cherry", "date", "elderberry"], perform the following operations: (a) Add "fig" at the end, (b) Insert "grape" at index 2, (c) Remove "cherry", (d) Pop the last element.
fruits = ["apple", "banana", "cherry", "date", "elderberry"]
print("Original:", fruits)
# (a) Add "fig" at the end
fruits.append("fig")
print("After append:", fruits)
# ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig']
# (b) Insert "grape" at index 2
fruits.insert(2, "grape")
print("After insert:", fruits)
# ['apple', 'banana', 'grape', 'cherry', 'date', 'elderberry', 'fig']
# (c) Remove "cherry"
fruits.remove("cherry")
print("After remove:", fruits)
# ['apple', 'banana', 'grape', 'date', 'elderberry', 'fig']
# (d) Pop the last element
popped = fruits.pop()
print("Popped:", popped) # fig
print("After pop:", fruits)
# ['apple', 'banana', 'grape', 'date', 'elderberry']
Output:
Q3. Write a program to sort a list of numbers in ascending and descending order without modifying the original list.
nums = [34, 12, 67, 5, 89, 23]
print("Original:", nums)
# Ascending — using sorted()
asc = sorted(nums)
print("Ascending:", asc) # [5, 12, 23, 34, 67, 89]
# Descending — using sorted() with reverse=True
desc = sorted(nums, reverse=True)
print("Descending:", desc) # [89, 67, 34, 23, 12, 5]
# Verify original is unchanged
print("Original after sorted:", nums) # [34, 12, 67, 5, 89, 23]
Output:
Q4. Use list comprehension to create: (a) Squares of numbers from 1 to 15, (b) A list of vowels from a given string, (c) A list of all numbers divisible by 3 in the range 1–50.
# (a) Squares of numbers from 1 to 15
squares = [x**2 for x in range(1, 16)]
print("Squares:", squares)
# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225]
# (b) Vowels from a given string
text = "Programming is fun"
vowels = [ch for ch in text.lower() if ch in "aeiou"]
print("Vowels:", vowels)
# ['o', 'a', 'i', 'i', 'u']
# (c) Numbers divisible by 3 in range 1–50
div_by_3 = [x for x in range(1, 51) if x % 3 == 0]
print("Divisible by 3:", div_by_3)
# [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]
Output:
Q5. Write a program to find the maximum and minimum values in a list without using the built-in max() and min() functions.
nums = [34, 12, 67, 5, 89, 23]
# Find maximum
maximum = nums[0]
for n in nums:
if n > maximum:
maximum = n
# Find minimum
minimum = nums[0]
for n in nums:
if n < minimum:
minimum = n
print("List:", nums)
print("Maximum:", maximum) # 89
print("Minimum:", minimum) # 5
Output:
Q6. Write a program to remove duplicate elements from a list while preserving the original order.
nums = [1, 2, 3, 2, 4, 1, 5, 3, 6, 5]
print("Original:", nums)
# Method: iterate and keep only first occurrence
unique = []
for n in nums:
if n not in unique:
unique.append(n)
print("After removing duplicates:", unique)
# [1, 2, 3, 4, 5, 6]
Output:
Q7. Write a program to merge two sorted lists into a single sorted list.
list1 = [1, 3, 5, 7, 9]
list2 = [2, 4, 6, 8, 10]
# Method 1: simple merge and sort
merged = sorted(list1 + list2)
print("Merged (simple):", merged)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Method 2: two-pointer merge (efficient — O(n))
i = j = 0
result = []
while i < len(list1) and j < len(list2):
if list1[i] < list2[j]:
result.append(list1[i])
i += 1
else:
result.append(list2[j])
j += 1
# Add remaining elements
result.extend(list1[i:])
result.extend(list2[j:])
print("Merged (two-pointer):", result)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output:
Q8. Write a program to perform linear search on a list. Return the index of the target if found, otherwise return -1.
def linear_search(lst, target):
"""Search for target in lst. Return index if found, else -1."""
for i in range(len(lst)):
if lst[i] == target:
return i
return -1
# Test the function
numbers = [10, 25, 30, 45, 50, 65, 80]
print("List:", numbers)
print("Search 45:", linear_search(numbers, 45)) # 3
print("Search 99:", linear_search(numbers, 99)) # -1
print("Search 10:", linear_search(numbers, 10)) # 0
Output:
Q9. Given a nested list matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], write a program to: (a) Print the diagonal elements, (b) Calculate the sum of all elements, (c) Flatten the matrix into a single list.
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# (a) Print diagonal elements
print("Diagonal elements:")
for i in range(len(matrix)):
print(matrix[i][i], end=" ")
# 1 5 9
print()
# (b) Sum of all elements
total = 0
for row in matrix:
for item in row:
total += item
print("Sum of all elements:", total) # 45
# (c) Flatten into a single list
flat = [item for row in matrix for item in row]
print("Flattened list:", flat)
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
Output:
Q10. Write a program to count the frequency of each element in a list and store the result in a dictionary.
nums = [1, 2, 3, 2, 1, 3, 3, 4, 2, 5, 1]
print("List:", nums)
freq = {}
for n in nums:
freq[n] = freq.get(n, 0) + 1
print("Frequency dictionary:", freq)
# {1: 3, 2: 3, 3: 3, 4: 1, 5: 1}
# Display nicely
for key in sorted(freq):
print(f" {key} appears {freq[key]} time(s)")
Output:
Related Chapters
- [CBSE Class 11 Computer Science Chapter 9 — Lists (Chapter Guide)](/class/11/computer-science/cbse-lists) — full concept notes with definitions, methods, and exam tips
- [CBSE Class 11 Computer Science Chapter 10 — Tuples and Dictionaries (Chapter Guide)](/class/11/computer-science/cbse-tuples-and-dictionaries) — next chapter
- [CBSE Class 11 Computer Science Chapter 10 — Tuples and Dictionaries Exercise Solutions](/class/11/computer-science/solutions/cbse-tuples-and-dictionaries-exercise-solutions) — practice the next chapter
