📘 Python Lecture: Lists in Python
1. Introduction to Lists
● A list is a collection of items stored in a single variable.
● Lists are:
○ Ordered → items have a fixed order
○ Mutable (changeable) → we can add, update, remove items
○ Allow duplicates → same item can appear more than once
○ Can store mixed data types → numbers, strings, even other lists
Example:
# List of integers
numbers = [10, 20, 30, 40]
# Mixed list
student = ["Amit", 17, "XII", 92.5]
# List of lists
matrix = [[1, 2], [3, 4], [5, 6]]
2. Creating Lists
Lists can be created in different ways:
# Empty list
empty_list = []
# List with elements
fruits = ["apple", "banana", "mango"]
# Using list() constructor
numbers = list((1, 2, 3, 4))
# From a string
chars = list("hello") # ['h', 'e', 'l', 'l', 'o']
# From a range
nums = list(range(5)) # [0, 1, 2, 3, 4]
3. Accessing Elements
● Use indexing (starts at 0).
● Negative indexing counts from the end.
fruits = ["apple", "banana", "mango", "orange"]
print(fruits[0]) # apple
print(fruits[-1]) # orange
● Slicing → extract part of a list.
print(fruits[1:3]) # ['banana', 'mango']
print(fruits[:2]) # ['apple', 'banana']
print(fruits[::2]) # ['apple', 'mango']
4. Modifying Lists
Lists are mutable → we can change items.
fruits = ["apple", "banana", "mango"]
# Change an element
fruits[1] = "grape"
print(fruits) # ['apple', 'grape', 'mango']
5. List Methods
🔹 Adding Elements
fruits = ["apple", "banana"]
fruits.append("mango") # Add at end
print(fruits) # ['apple', 'banana', 'mango']
fruits.insert(1, "grape") # Insert at index 1
print(fruits) # ['apple', 'grape', 'banana', 'mango']
fruits.extend(["orange", "kiwi"]) # Add multiple
print(fruits) # ['apple', 'grape', 'banana', 'mango', 'orange', 'kiwi']
🔹 Removing Elements
fruits = ["apple", "banana", "mango", "orange"]
fruits.remove("banana") # Remove by value
print(fruits) # ['apple', 'mango', 'orange']
fruits.pop(1) # Remove by index
print(fruits) # ['apple', 'orange']
fruits.pop() # Remove last item
print(fruits) # ['apple']
fruits.clear() # Empty the list
print(fruits) # []
🔹 Searching & Counting
fruits = ["apple", "banana", "apple", "mango"]
print(fruits.index("mango")) # 3 (first occurrence)
print(fruits.count("apple")) # 2
🔹 Sorting & Reversing
numbers = [3, 1, 4, 2, 5]
numbers.sort() # Sort ascending
print(numbers) # [1, 2, 3, 4, 5]
numbers.sort(reverse=True) # Sort descending
print(numbers) # [5, 4, 3, 2, 1]
numbers.reverse() # Reverse order
print(numbers) # [1, 2, 3, 4, 5]
🔹 Copying Lists
fruits = ["apple", "banana", "mango"]
# Shallow copy
copy1 = fruits.copy()
copy2 = list(fruits)
print(copy1) # ['apple', 'banana', 'mango']
print(copy2) # ['apple', 'banana', 'mango']
🔹 Other Useful Methods
numbers = [10, 20, 30, 40]
print(len(numbers)) # 4 (length of list)
print(max(numbers)) # 40
print(min(numbers)) # 10
print(sum(numbers)) # 100
6. Iterating Through Lists
fruits = ["apple", "banana", "mango"]
# For loop
for fruit in fruits:
print(fruit)
# While loop with index
i=0
while i < len(fruits):
print(fruits[i])
i += 1
7. List Comprehensions (Quick Creation)
# Squares of numbers
squares = [x**2 for x in range(1, 6)]
print(squares) # [1, 4, 9, 16, 25]
# Even numbers
evens = [x for x in range(10) if x % 2 == 0]
print(evens) # [0, 2, 4, 6, 8]
8. Practice Questions
1. Create a list of 10 numbers. Print their sum and average.
2. Write a program to remove duplicates from a list.
3. Create a list of student names and sort them alphabetically.
4. Input 5 numbers from the user and store them in a list. Then
display the maximum and minimum numbers.
5. Write a Python program to count how many times each
element appears in a list.
6. Use list comprehension to generate a list of all multiples of
3 between 1–50.
7. Reverse a list without using the reverse() method.