1. Introduction to Lists
A list is an ordered collection of items enclosed in square brackets []. Lists can contain elements of different data types (integers, floats, strings, other lists, etc.).
Lists are mutable, meaning their elements can be changed after creation.
2. Creating Lists
# Empty list
empty_list = []
# List of integers
numbers = [1, 2, 3, 4, 5]
# List of strings
fruits = ["apple", "banana", "cherry"]
# Mixed data types
mixed = [10, "Hello", 3.14, True]
# Nested list
nested = [[1, 2], [3, 4], [5, 6]]
# Using list() constructor
chars = list("Python") # ["P", "y", "t", "h", "o", "n"]
# Range to list
nums = list(range(5)) # [0, 1, 2, 3, 4]3. List Indexing
fruits = ["apple", "banana", "cherry", "date"]
# Index: 0 1 2 3
# Negative: -4 -3 -2 -1
print(fruits[0]) # apple
print(fruits[2]) # cherry
print(fruits[-1]) # date (last element)
print(fruits[-3]) # banana4. List Slicing
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(nums[2:6]) # [2, 3, 4, 5]
print(nums[:4]) # [0, 1, 2, 3]
print(nums[5:]) # [5, 6, 7, 8, 9]
print(nums[::2]) # [0, 2, 4, 6, 8]
print(nums[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]5. Lists are Mutable
fruits = ["apple", "banana", "cherry"]
fruits[1] = "blueberry" # Change element
print(fruits) # ["apple", "blueberry", "cherry"]
# Assigning to a slice
nums = [1, 2, 3, 4, 5]
nums[1:3] = [20, 30]
print(nums) # [1, 20, 30, 4, 5]6. Traversing a List
fruits = ["apple", "banana", "cherry"]
# By element
for fruit in fruits:
print(fruit)
# By index
for i in range(len(fruits)):
print(f"Index {i}: {fruits[i]}")
# Using enumerate
for i, fruit in enumerate(fruits):
print(i, fruit)7. List Operators
# Concatenation (+)
a = [1, 2, 3]
b = [4, 5, 6]
print(a + b) # [1, 2, 3, 4, 5, 6]
# Repetition (*)
print([0] * 5) # [0, 0, 0, 0, 0]
# Membership (in)
print(3 in a) # True
print(7 not in a) # True8. List Methods
| Method | Description | Example | Result |
|---|---|---|---|
| append(x) | Adds x at the end | [1,2].append(3) | [1,2,3] |
| extend(seq) | Adds all elements of seq | [1,2].extend([3,4]) | [1,2,3,4] |
| insert(i, x) | Inserts x at index i | [1,3].insert(1,2) | [1,2,3] |
| remove(x) | Removes first occurrence of x | [1,2,3,2].remove(2) | [1,3,2] |
| pop(i) | Removes and returns element at i | [1,2,3].pop(1) | 2, list=[1,3] |
| index(x) | Returns index of first occurrence | [1,2,3].index(2) | 1 |
| count(x) | Counts occurrences of x | [1,2,2,3].count(2) | 2 |
| sort() | Sorts list in place | [3,1,2].sort() | [1,2,3] |
| reverse() | Reverses list in place | [1,2,3].reverse() | [3,2,1] |
| copy() | Returns a shallow copy | [1,2].copy() | [1,2] |
| clear() | Removes all elements | [1,2].clear() | [] |
9. Nested Lists
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[0]) # [1, 2, 3]
print(matrix[1][2]) # 6
# Traversing nested list
for row in matrix:
for element in row:
print(element, end=" ")
print()10. List Comprehension
List comprehension provides a concise way to create lists.
# Basic syntax: [expression for item in iterable if condition]
# Squares of numbers
squares = [x**2 for x in range(10)]
print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# Even numbers only
evens = [x for x in range(20) if x % 2 == 0]
print(evens) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
# Nested list comprehension
matrix = [[i*j for j in range(1, 4)] for i in range(1, 4)]
print(matrix) # [[1, 2, 3], [2, 4, 6], [3, 6, 9]]11. Copying Lists
# Shallow copy (two ways)
a = [1, 2, 3]
b = a.copy()
c = a[:]
# For nested lists, use deep copy
import copy
nested = [[1, 2], [3, 4]]
deep = copy.deepcopy(nested)12. Revision Questions and Answers
Very Short Answer Questions
1. What is a list in Python?
An ordered, mutable collection of elements enclosed in square brackets.
2. Are lists mutable or immutable?
Mutable.
3. Which method adds an element at the end of a list?
append().
4. Which method removes and returns the last element?
pop().
5. How do you create a list of squares from 1 to 10 in one line?
[x**2 for x in range(1, 11)]
Short Answer Questions
1. Differentiate between append() and extend().
append() adds its argument as a single element to the end of the list. extend() adds all elements of an iterable to the end. Example: [1,2].append([3,4]) gives [1,2,[3,4]], while [1,2].extend([3,4]) gives [1,2,3,4].
2. Explain list slicing with examples.
Slicing extracts a portion using list[start:stop:step]. Example: [0,1,2,3,4][1:4] returns [1,2,3]. Negative step reverses: [0,1,2,3,4][::-1] returns [4,3,2,1,0].
Long Answer Questions
1. Write a Python program to find the second largest element in a list.
nums = [10, 5, 8, 20, 3, 15]; nums.sort(); print("Second largest:", nums[-2]) # 15
2. Write a program using list comprehension to separate even and odd numbers from a given list.
nums = [1,2,3,4,5,6,7,8,9,10]; evens = [x for x in nums if x%2==0]; odds = [x for x in nums if x%2!=0]; print("Evens:", evens); print("Odds:", odds)