Computer Science

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

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() and len() are used to calculate the mean.
  • Linear search checks elements one by one.
  • count() returns the frequency of an element.