Python Lists
🐍 Python Lists — Full Tutorial
A Python Lists is an ordered, changeable (mutable) collection that can store multiple items of different data types.
Lists are written using square brackets []:
1. Creating a List
2. Accessing List Items (Indexing)
Indexes start from 0:
Negative indexing:
3. Slicing Lists
Extract a portion of the list:
4. Modifying List Items
Lists are mutable, meaning you can change values.
5. Adding Items to a List
| Method | Action |
|---|---|
append() |
Adds item at end |
insert() |
Adds item at specific index |
extend() |
Adds multiple items |
Examples:
6. Removing Items
| Method | Action |
|---|---|
remove() |
remove by value |
pop() |
remove by index (default removes last item) |
del |
delete using index or whole list |
clear() |
empty the list |
Examples:
7. Looping Through a List
With index:
8. Checking if Item Exists
9. Sorting Lists
Sorting strings:
10. Copying Lists (Important)
or:
11. Joining Lists
or:
12. Built-in List Functions
| Function | Purpose |
|---|---|
len() |
count items |
max() |
largest value |
min() |
smallest value |
sum() |
sum of numbers |
Example:
13. List Comprehension (Advanced but important)
A short way to create lists:
Filter example:
