# Python Lists – Complete Notes
## 1. Introduction to Lists
- A list is an ordered, mutable, and iterable collection.
- Lists allow duplicate elements.
- Syntax: `my_list = [1, 2, 3]`
## 2. Creating Lists
- Empty list: `[]` or `list()`
- With elements: `[1, 2, 3]`
- Mixed data types: `[1, "hello", 3.5]`
- Nested list: `[1, [2, 3], 4]`
## 3. Accessing List Elements
- Positive indexing: `list[0]`
- Negative indexing: `list[-1]`
- Nested access: `list[1][0]`
## 4. List Slicing
- `list[start:end]`
- `list[:end]`, `list[start:]`
- Step slicing: `list[start:end:step]`
## 5. Updating Lists
- Modify an element: `list[0] = 10`
- Modify slice: `list[1:3] = [20, 30]`
## 6. Adding Elements
- `[Link](x)` → adds at end
- `[Link](i, x)` → inserts at index
- `[Link]([a, b])` → adds multiple items
## 7. Removing Elements
- `[Link](x)` → removes first occurrence
- `[Link](i)` → removes index
- `[Link]()` → removes last element
- `del list[i]`
- `[Link]()` → empty list
## 8. Searching in Lists
- `x in list`
- `[Link](x)`
- `[Link](x)`
## 9. Sorting Lists
- `[Link]()`
- `[Link](reverse=True)`
- `sorted(list)` → returns new list
## 10. List Functions
- `len(list)`
- `max(list)`
- `min(list)`
- `sum(list)`
- `[Link]()`
## 11. List Comprehension
- `[x for x in range(10)]`
- `[x*x for x in nums if x%2==0]`
## 12. Iterating Lists
- Using for loop
- Using while loop
- Using enumerate()
## 13. Nested Lists
- 2D list example:
```
matrix = [
[1, 2, 3],
[4, 5, 6]
```
## 14. Important List Methods
- append()
- extend()
- insert()
- remove()
- pop()
- clear()
- sort()
- reverse()
- copy()
## 15. Difference Between List & Tuple
- List is mutable, tuple is immutable
- List uses `[]`, tuple uses `()`
## 16. Common Errors
- IndexError
- ValueError
- TypeError
## 17. Practical Programs
1. Reverse list
2. Find largest number
3. Remove duplicates
4. Merge two lists
5. List comprehension examples