How to Update an Array in Python

I was working on a project where I needed to update a list of customer order quantities stored in a Python array. The challenge was simple: I had to modify some values dynamically based on new data coming from an API.

If you’ve ever worked with arrays in Python, you probably already know that there are multiple ways to update or modify their elements. However, the best method depends on the type of array you’re using: a built-in Python list, an array from the array module, or a NumPy array.

In this tutorial, I’ll show you three simple ways to update an array in Python, using practical, real-world examples. I’ll walk you through each method step-by-step, share full code examples, and explain how I personally use these techniques in my daily Python development work.

Method 1 – Update Array Elements Using Indexing in Python

When working with arrays in Python, the easy way to update an element is by directly accessing it using its index. This approach is ideal when you know the exact position of the element you want to change. It’s simple, efficient, and works perfectly for small to medium-sized arrays.

Here’s how I usually do it:

# Import the array module
from array import array

# Create an array of integers
sales_data = array('i', [1200, 1500, 1800, 2000, 2200])

print("Original Sales Data:", sales_data.tolist())

# Update the third element (index 2)
sales_data[2] = 1900

print("Updated Sales Data:", sales_data.tolist())

I executed the above example code and added the screenshot below.

python array update

In this example, I created an array of monthly sales numbers for a small retail store in New York. Then, I updated the third element (representing March) from 1800 to 1900. The .tolist() method is used just to make the array output more readable. You can see how easy it is to modify array values directly using indexing.

Update Multiple Elements at Once

Sometimes, you may need to update multiple array elements at once, for example, when applying a price adjustment or tax increase.

Here’s how you can do that easily:

from array import array

# Create an array of product prices
product_prices = array('f', [9.99, 14.99, 19.99, 24.99])

print("Original Prices:", product_prices.tolist())

# Apply a 10% discount to all prices
for i in range(len(product_prices)):
    product_prices[i] = round(product_prices[i] * 0.9, 2)

print("Discounted Prices:", product_prices.tolist())

In this example, I applied a 10% discount to all product prices. Using a simple loop, I accessed each element by index and updated its value. This approach gives you full control over how each element is modified, which is perfect for business logic like discounts or tax calculations.

Method 2 – Update Arrays Using Python List Methods

If you’re using Python lists as arrays (which is very common in real-world projects), you can take advantage of built-in list methods such as append(), insert(), and extend(). These methods are great when you need to add or replace elements dynamically.

Here’s a practical example:

# Create a list of employee IDs
employee_ids = [101, 102, 103, 104, 105]

print("Original Employee IDs:", employee_ids)

# Update a specific element
employee_ids[2] = 203

# Add a new employee ID
employee_ids.append(106)

# Insert a new ID at a specific position
employee_ids.insert(1, 999)

print("Updated Employee IDs:", employee_ids)

I executed the above example code and added the screenshot below.

python update array

In this case, I replaced one employee ID, added a new one at the end, and inserted another at a specific position. This method is flexible and beginner-friendly, especially if you’re just starting with Python arrays.

Replace Multiple Values in a Python List

Sometimes, you may want to replace multiple values that meet certain conditions — for example, updating all negative numbers to zero.

Here’s an example that demonstrates this:

temperatures = [72, 68, -999, 75, -999, 80]

print("Original Temperatures:", temperatures)

# Replace all invalid values (-999) with 70
temperatures = [70 if temp == -999 else temp for temp in temperatures]

print("Cleaned Temperatures:", temperatures)

In this example, the value -999 represents missing temperature data (a common placeholder in datasets). I used a list comprehension to replace all invalid readings with 70. This method is clean and efficient, especially when working with larger datasets or real-time data streams.

Method 3 – Update NumPy Arrays in Python

When dealing with large datasets or numerical computations, NumPy arrays are the best choice. They are faster, memory-efficient, and come with built-in vectorized operations that make updates incredibly easy.

Let’s look at a simple example:

import numpy as np

# Create a NumPy array of weekly sales
weekly_sales = np.array([2500, 2700, 3000, 3200, 3100])

print("Original Weekly Sales:", weekly_sales)

# Update the second element
weekly_sales[1] = 2800

print("Updated Weekly Sales:", weekly_sales)

I executed the above example code and added the screenshot below.

update array python

Just like with regular arrays, you can access and modify elements using their index. But the real power of NumPy comes from vectorized updates, which allow you to modify multiple elements at once.

Update Multiple Elements with Conditions in NumPy

One of my favorite features in Python’s NumPy is conditional updating. You can update elements based on a condition without writing any loops.

Here’s how that works:

import numpy as np

# Create a NumPy array of student scores
scores = np.array([55, 72, 89, 45, 67, 92, 38])

print("Original Scores:", scores)

# Update all scores below 50 to 50 (minimum passing score)
scores[scores < 50] = 50

print("Adjusted Scores:", scores)

In this example, I updated all failing student scores (below 50) to the minimum passing score. This kind of operation is extremely efficient in Python because NumPy performs the update internally using optimized C-based operations.

Update NumPy Arrays with Arithmetic Operations

You can also update NumPy arrays using arithmetic operations directly — for example, applying a percentage increase or scaling values.

import numpy as np

# Create a NumPy array of hourly wages
wages = np.array([15.50, 18.75, 20.00, 22.50])

print("Original Wages:", wages)

# Apply a 5% raise to all employees
wages = wages * 1.05

print("Wages After Raise:", wages)

Here, I gave all employees a 5% raise by multiplying the entire array by 1.05. This is one of the biggest advantages of NumPy: you can update thousands (or even millions) of values instantly without writing loops.

Bonus Tip – Update Arrays Using Slicing in Python

Another useful trick is to update array elements using slicing. This allows you to modify a specific range of elements in one go.

from array import array

# Create an array of integers
numbers = array('i', [10, 20, 30, 40, 50, 60])

print("Original Numbers:", numbers.tolist())

# Replace the middle three elements
numbers[2:5] = array('i', [300, 400, 500])

print("Updated Numbers:", numbers.tolist())

Here, I replaced the middle three elements with new values using slicing syntax. This technique is both elegant and efficient, especially when you need to replace consecutive elements in an array.

Common Mistakes When Updating Arrays in Python

Over the years, I’ve seen many beginners make a few common mistakes when updating arrays in Python. Here are a few to watch out for:

  • Using the wrong array type: Remember that Python lists, array.array, and NumPy arrays behave differently.
  • Not converting data types properly: NumPy arrays often require consistent data types.
  • Forgetting to copy arrays: When you assign one array to another, you might accidentally create a reference instead of a copy.

Understanding these nuances will help you avoid bugs and ensure your code behaves as expected.

We explored several methods, from simple indexing to using list comprehensions and NumPy operations. Each approach has its own use case, and the right choice depends on the size and structure of your data.

If you’re working with small datasets or simple lists, Python’s built-in arrays or lists are more than enough. But if you’re handling large datasets, like financial data or scientific computations, NumPy arrays are your best friend.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.