Python Basics
Python Lists
1. What is a List?
- A data type that stores multiple elements in a single variable
- Also called a collection — elements are ordered
- Elements can be any data type and don't need to match each other
- Short form / type name in Python:
list
x = [1, 2, 3] # list of ints
x = [1, 2, True, 3.4, "hello"] # mixed types — totally fine
x = [] # empty list — valid
x = [1, [2, 3], "hi"] # list inside a list — valid
2. Indexing — Accessing Elements
- Every element has a position (index), starting at 0
- Access using square bracket notation:
list[index]
x = [1, 2, 3, True, 3.4, "hello"]
# 0 1 2 3 4 5 ← positive indices
# -6 -5 -4 -3 -2 -1 ← negative indices
print(x[0]) # 1 (first element)
print(x[5]) # hello (last element)
print(x[-1]) # hello (last element via negative index)
print(x[-2]) # 3.4
Negative Indexing
-1= last element,-2= second last, and so on- Quick way to access elements from the end without knowing the length
⚠️ Accessing an index that doesn't exist →
IndexError: list index out of range
3. Modifying Elements
x[0] = 4 # changes first element to 4
⚠️ You cannot assign to an index that doesn't exist yet — use
append()instead
4. len() — Length of a List
x = [1, 2, 3, 4, 5]
print(len(x)) # 5
- Also works on strings (counts characters)
- Last valid index =
len(x) - 1
5. List Methods
append(item) — Add to End
x.append("last") # adds "last" to the end of x
x.append(2) # adds 2 after that
pop() — Remove & Return Last Element
popped = x.pop() # removes last element and stores it
x.pop() # removes last element without storing
remove(item) — Remove First Occurrence
x.remove(1) # removes the first 1 found in the list
⚠️ Raises an error if the element doesn't exist in the list
count(item) — Count Occurrences
x.count(1) # returns how many times 1 appears
Guide:
Truecounts as1andFalsecounts as0in Python
index(item) — Find First Occurrence Index
x.index(3) # returns the index of the first 3
⚠️ Raises an error if the element doesn't exist in the list
extend(list) — Add All Elements of Another List
x = [1, 2, 3]
y = [4, 5]
x.extend(y) # x is now [1, 2, 3, 4, 5]
# y is unchanged
6. Checking if an Element Exists — in Operator
x = [1, 2, 3]
print(5 in x) # False
print(1 in x) # True
Alternative using count():
x.count(5) > 0 # True if 5 exists, False if not
Best practice: use
in— it's cleaner and more readable
7. Combining Lists
Using + — Creates a New List
x = [1, 2, 3]
y = [4, 5]
combined = x + y # [1, 2, 3, 4, 5]
# x and y are unchanged
Using extend() — Modifies the Original
x.extend(y) # adds y's elements to end of x (x is changed, y is not)
8. Nested Lists (Multi-Dimensional Lists)
- A list can contain other lists as elements
- Access inner elements by chaining index operations
lst = [[1, 2], [3, 4], [5, 6, [100]]]
lst[2] # [5, 6, [100]] — third element (a list)
lst[-1] # [5, 6, [100]] — same, using negative index
lst[-1][1] # 6 — second element of last list
lst[0][2][0] # 100 — element 0 of the nested list inside index 0 and 2
Chaining indexes:
lst[a][b][c]— access level by level⚠️ Trying to index a non-list (e.g.
100[0]) raises aTypeError
9. Lists vs Strings
| Feature | List | String |
|---|---|---|
Indexing with [] |
✅ | ✅ |
len() |
✅ | ✅ |
| Modify element in-place | ✅ x[0] = 4 |
❌ TypeError |
append() |
✅ | ❌ |
| Ordered collection | ✅ | ✅ (of characters) |
10. Key Takeaways & Recap
- Lists store multiple elements of any type in a single variable
- Indexing starts at 0; negative indices count from the end (
-1= last) - Accessing an out-of-range index →
IndexError - Use
append()to add,pop()to remove from end,remove()for specific items inoperator is the cleanest way to check if an element exists+creates a new combined list;extend()modifies the original- Lists can be nested — use chained indexes
lst[a][b]to access inner elements - Unlike lists, strings are immutable — you cannot modify characters in place