Arrays Explained: Indexing, Searching, and Updating
Arrays are one of the first data structures most developers learn. In Python, you will usually work with `list`, which behaves like a dynamic array for most everyday purposes. An array stores items in order. That simple idea makes arrays useful for a huge number of problems:
- lists of users
- rows from a CSV file
- search results
- tasks in a queue-like workflow
- UI items to render on a page
- numbers to sort, filter, or analyze Arrays are simple, but they still have tradeoffs.
The Mental Model
Think of an array as a row of numbered boxes. Each box holds a value. The number of the box is its index.
colors = ["red", "green", "blue"]
The indexes are: ```plain text index: 0 1 2 value: "red" "green" "blue"
Most programming languages use zero-based indexing. That means the first item is at index \`0\`, not index \`1\`.
```python
print(colors[0]) # red
print(colors[1]) # green
print(colors[2]) # blue
Once that indexing model clicks, a lot of array problems become easier.
Reading by Index
Reading an item by index is fast.
names = ["maya", "alex", "tristan", "jordan"]
third_name = names[2]
This is O(1), or constant time, because Python can go directly to that position. That is one of the biggest strengths of arrays. If you know the index, access is simple and efficient.
Searching an Array
Searching is different. If you do not know where an item is, you may have to check each item one by one.
def contains_name(names, target):
for name in names:
if name == target:
return True
return False
This is O(n) in the worst case. If the target is near the beginning, the function returns quickly. If the target is at the end or not present, the function scans the whole list. For small lists, this is fine. For large lists or repeated lookups, another data structure like a set or dictionary may be better.
Updating an Array
Updating an item by index is also straightforward.
scores = [80, 90, 75]
scores[2] = 85
print(scores) # [80, 90, 85]
If you know the index, updating is O(1). But inserting or deleting from the middle can be more expensive.
numbers = [1, 2, 4, 5]
numbers.insert(2, 3)
print(numbers) # [1, 2, 3, 4, 5]
To insert `3` at index `2`, Python has to shift later items to make room. That shifting is why inserting into the middle of a list is usually O(n).
Appending to the End
Adding to the end of a Python list is usually efficient.
tasks = []
tasks.append("write article")
tasks.append("review draft")
tasks.append("publish post")
Appending is usually treated as O(1) on average. This makes lists great for collecting results as you loop through data.
def only_even(numbers):
evens = []
for number in numbers:
if number % 2 == 0:
evens.append(number)
return evens
That pattern shows up constantly in practical Python code.
Common Mistakes
Mistake 1: Forgetting zero-based indexing
The first item is index `0`. The last item is index `len(items) - 1`.
items = ["a", "b", "c"]
print(items[len(items) - 1]) # c
Python also lets you use negative indexes:
print(items[-1]) # c
That is convenient, but it is still important to understand the underlying index positions.
Mistake 2: Using a list for repeated membership checks
This works:
if user_id in user_ids:
print("allowed")
But if `user_ids` is a large list and this check happens often, a set may be better.
allowed_ids = set(user_ids)
if user_id in allowed_ids:
print("allowed")
Arrays are good at ordered storage. They are not always the best choice for fast membership lookup.
Mistake 3: Mutating a list while looping over it
This can create bugs:
for task in tasks:
if task.done:
tasks.remove(task)
A safer pattern is to build a new list:
tasks = [task for task in tasks if not task.done]
When in doubt, avoid changing the shape of a list while iterating over it.
Where This Shows Up in Real Projects
Arrays and lists are everywhere:
- rendering cards in a frontend UI
- storing form errors
- processing CSV rows
- collecting database results
- filtering search results
- keeping ordered steps in a workflow Even when a framework hides the details, arrays are usually nearby. Understanding their strengths and weaknesses makes your everyday code better.
Key Takeaways
- Arrays store items in order.
- Reading or updating by index is fast.
- Searching an unsorted array is usually O(n).
- Inserting or deleting in the middle can require shifting items.
Lists are great for ordered data, but sets and dictionaries are often better for lookup-heavy code.
Related Articles
What Are Data Structures and Algorithms?
- Big O Notation Explained With Python Examples
- Hash Tables Explained Simply