List - Python
1. What is a List in Python?
A list in Python is a collection of ordered items.
It stores multiple values in one variable.
It allows duplicate values.
It is mutable (we can change it).
It keeps insertion order.
Syntax
list_name = [item1, item2, item3]
Example
students = ["Ravi", "Aman", "Neha"]
marks = [78, 85, 90]
Here:
students is a list of strings.
marks is a list of integers.
2. Why Lists Are Important (Real-Life Thinking)
Imagine you are:
- Managing student marks in school
- Storing products in an online shop
- Keeping track of daily expenses
- Maintaining attendance records
In all these cases, we need to store multiple related values.
That’s where lists are used.
3. Creating Lists
1. Empty List
my_list = []
2. List with Values
numbers = [10, 20, 30, 40]
3. Mixed Data Type List
data = ["Rajat", 25, True]
(Allowed, but in real applications we usually keep similar types together.)
4. Accessing List Elements
Lists use index numbers.
Index starts from 0.
Negative index starts from -1 (from end).
fruits = ["Apple", "Banana", "Mango", "Orange"]
print(fruits[0]) # Apple
print(fruits[-1]) # Orange
5. List Slicing
Used to access multiple elements.
Syntax
list[start:stop:step]
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # [20, 30, 40]
print(numbers[:3]) # [10, 20, 30]
print(numbers[::2]) # [10, 30, 50]
6. Modifying Lists (Because Lists Are Mutable)
students = ["Ravi", "Aman", "Neha"]
students[1] = "Sita"
print(students)
Output:
['Ravi', 'Sita', 'Neha']
7. Important List Methods (Very Important for Exams)
1. append()
Adds element at end.
numbers = [1, 2, 3]
numbers.append(4)
2. insert()
Insert at specific position.
numbers.insert(1, 100)
3. remove()
Removes by value.
numbers.remove(100)
4. pop()
Removes by index.
numbers.pop(2)
5. sort()
numbers.sort()
6. reverse()
numbers.reverse()
7. len()
len(numbers)
8. Real Life Example 1 – Student Marks System
Problem: Store marks of 5 students and calculate average.
Solution
marks = [80, 75, 90, 60, 85]
total = sum(marks)
average = total / len(marks)
print("Total Marks:", total)
print("Average:", average)
Explanation:
sum() adds all values.
len() gives number of students.
Average = total / count
This is exactly how school result systems work.
9. Real Life Example 2 – Grocery Bill Calculator
Problem: Store prices of items and calculate total bill.
prices = [50, 120, 30, 200]
total_bill = sum(prices)
print("Total Bill:", total_bill)
Practical Use: Billing software in shops works like this internally.
10. Real Life Example 3 – Attendance System
Problem: Count how many students are present.
attendance = ["P", "A", "P", "P", "A", "P"]
present_count = attendance.count("P")
print("Total Present:", present_count)
count() method counts occurrences.
11. Real Life Example 4 – Remove Duplicate Phone Numbers
numbers = [9876, 1234, 9876, 4567, 1234]
unique_numbers = []
for num in numbers:
if num not in unique_numbers:
unique_numbers.append(num)
print(unique_numbers)
Used in:
- Contact management
- Spam filtering
- Data cleaning
12. Academic Exam Based Questions (With Solution)
Q1. Write a program to find largest number in a list.
numbers = [45, 12, 89, 34, 67]
largest = numbers[0]
for num in numbers:
if num > largest:
largest = num
print("Largest number:", largest)
Logic:
Assume first element is largest.
Compare with others.
Update if bigger found.
Q2. Write a program to count even and odd numbers.
numbers = [10, 15, 22, 33, 40]
even = 0
odd = 0
for num in numbers:
if num % 2 == 0:
even += 1
else:
odd += 1
print("Even:", even)
print("Odd:", odd)
Q3. Reverse a List Without Using reverse()
numbers = [1, 2, 3, 4, 5]
reversed_list = numbers[::-1]
print(reversed_list)
Q4. Input 5 numbers from user and store in list
numbers = []
for i in range(5):
num = int(input("Enter number: "))
numbers.append(num)
print(numbers)
Q5. Search Element in List
numbers = [10, 20, 30, 40]
search = 30
if search in numbers:
print("Found")
else:
print("Not Found")
13. Nested Lists (Very Important for Advanced Questions)
A list inside another list.
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[1][2]) # 6
Used in:
- Tables
- Game boards
- Marks sheet (multiple subjects)
14. List Comprehension (Advanced but Important)
Short way to create lists.
squares = [x*x for x in range(1, 6)]
print(squares)
Output:
[1, 4, 9, 16, 25]
15. Difference Between List and Tuple (Exam Important)
Feature List Tuple
Mutable Yes No
Syntax [] ()
Performance Slower Faster
Use Case Changeable data Fixed data
16. Common Mistakes Students Make
- 1. Index out of range error
- 2. Forgetting list is mutable
- 3. Confusing remove() and pop()
- 4. Using wrong slicing
17. Final Practical Mini Project – Expense Tracker
expenses = []
while True:
amount = input("Enter expense (or 'q' to quit): ")
if amount == 'q':
break
expenses.append(float(amount))
print("Total Expenses:", sum(expenses))
print("Number of Entries:", len(expenses))