Python Nested Lists & Important Programs (Part 4) | Nested Lists, Maximum, Minimum, Mean, Linear Search & Frequency Count | CBSE Class 11 Computer Science
Class 11 · Computer Science
Python Nested Lists & Important Programs (Part 4)
A nested list is a list that contains one or more lists as its elements. Nested lists are useful for representing two-dimensional data such as marksheets, seating arrangements, matrices, and tables. This article also covers important CBSE practical programs based on lists.
Learning Objectives
- Understand nested lists.
- Access nested list elements.
- Traverse nested lists using loops.
- Write important CBSE list programs.
- Apply list processing techniques.
What is a Nested List?
A nested list is a list whose elements are themselves lists.
Example
marks=[
[85,90,88],
[78,81,75],
[92,95,89]
]
Here, the outer list contains three inner lists.
Accessing Nested List Elements
Use two indices to access an element:
marks=[
[85,90,88],
[78,81,75],
[92,95,89]
]
print(marks[0][1])
print(marks[2][0])
Output
90
92
Traversing a Nested List
marks=[
[85,90],
[78,81],
[92,95]
]
for row in marks:
for value in row:
print(value,end=" ")
print()
Output
85 90
78 81
92 95
Program 1: Find the Maximum Value in a List
numbers=[12,45,78,23,56]
maximum=max(numbers)
print("Maximum =",maximum)
Output
Maximum = 78
Program 2: Find the Minimum Value in a List
numbers=[12,45,78,23,56]
minimum=min(numbers)
print("Minimum =",minimum)
Output
Minimum = 12
Program 3: Find the Mean (Average)
marks=[75,80,85,90,95]
mean=sum(marks)/len(marks)
print("Mean =",mean)
Output
Mean = 85.0
Program 4: Linear Search
Linear search checks each element one by one until the required element is found.
numbers=[12,45,78,23,56]
key=int(input("Enter number to search: "))
found=False
for item in numbers:
if item==key:
found=True
break
if found:
print("Element Found")
else:
print("Element Not Found")
Program 5: Linear Search with Position
numbers=[12,45,78,23,56]
key=int(input("Enter number: "))
if key in numbers:
print("Position =",numbers.index(key))
else:
print("Not Found")
Program 6: Count Frequency of an Element
numbers=[10,20,10,30,20,10]
key=int(input("Enter number: "))
print("Frequency =",numbers.count(key))
Program 7: Count Frequency of All Elements
numbers=[10,20,10,30,20,10]
visited=[]
for item in numbers:
if item not in visited:
print(item,"=",numbers.count(item))
visited.append(item)
Output
10 = 3
20 = 2
30 = 1
Program 8: Find the Largest Number Without max()
numbers=[12,45,78,23,56]
largest=numbers[0]
for item in numbers:
if item>largest:
largest=item
print("Largest =",largest)
Program 9: Find the Smallest Number Without min()
numbers=[12,45,78,23,56]
smallest=numbers[0]
for item in numbers:
if item
Program 10: Calculate Sum Without sum()
numbers=[12,45,78,23,56]
total=0
for item in numbers:
total=total+item
print("Sum =",total)
Real-Life Applications
- Student marksheets.
- Attendance registers.
- Shopping carts.
- Sales reports.
- Employee salary records.
- Class-wise result analysis.
- Two-dimensional data representation.
Common Programming Mistakes
- Using a single index for nested lists.
- Forgetting to initialize variables before loops.
- Using
index()without checking whether the value exists. - Dividing by zero while calculating the mean of an empty list.
- Using
max()ormin()on an empty list.
CBSE Important Programs
- Find the maximum value in a list.
- Find the minimum value in a list.
- Calculate the mean of list elements.
- Perform linear search.
- Display the position of an element.
- Count the frequency of an element.
- Count the frequency of all elements.
- Traverse a nested list.
- Display all elements of a nested list.
- Find the largest and smallest values without using built-in functions.
Viva Questions
- What is a nested list?
- How do you access an element in a nested list?
- What is linear search?
- What is the time complexity of linear search?
- How is the mean of a list calculated?
- Which function returns the maximum value?
- Which function returns the minimum value?
- Which method counts the frequency of an element?
- Can nested lists contain different data types?
- Give two applications of nested lists.
Exam Tips
- Remember that nested lists require two indices.
- Always check whether an element exists before using
index(). - Practice writing linear search without using the
inoperator. - Understand the difference between built-in functions and manual logic.
- Trace the program using dry runs before execution.
Quick Revision
- A nested list is a list inside another list.
- Use two indices to access nested list elements.
max()returns the largest value.min()returns the smallest value.sum()andlen()are used to calculate the mean.- Linear search checks elements one by one.
count()returns the frequency of an element.