Python - Update in Array Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The array module provides support for arrays, and understanding how to update elements within these arrays is essential for efficient data manipulation. Updating elements in an array can be done in several ways, including direct assignment, slicing and using built-in methods. Let's explore these methods:Direct Assignment to Update an Array ElementThe simplest way to update an array element is by accessing the element using its index and assigning a new value. This works exactly like updating elements in a list. Python # Original array arr = array.array('i', [1, 2, 3, 4, 5]) # Update the element at index 2 (third element) arr[2] = 10 print(arr) Explanation:We accessed the element at index 2 (which is 3) and assigned a new value 10 to it.After updating, the array reflects the change and the element at index 2 is now 10.Using Slicing to Update Multiple Array ElementsYou can also update multiple elements in an array using slicing. This method is useful if you want to replace a range of elements in the array. Python # Original array arr = array.array('i', [1, 2, 3, 4, 5]) # Update the elements at indices 1 to 3 (second to fourth elements) arr[1:4] = array.array('i', [20, 30, 40]) print(arr) Explanation:We used slicing to select elements from index 1 to 3 ([2, 3, 4]) and replaced them with a new array [20, 30, 40].The array is updated to reflect the new values at indices 1 to 3.Using Built-in Methods to Modify the ArrayPython’s array module offers various methods for modifying arrays. Some of the most commonly used methods are:append(): Adds a single element to the end of the array.extend(): Adds multiple elements to the end of the array.insert(): Inserts a new element at a specified position.remove(): Removes the first occurrence of a specified value.pop(): Removes and returns an element at a given index. Python arr = array.array('i', [1, 2, 3]) arr.append(4) print(arr) arr.insert(2, 10) # Insert 10 at index 2 print(arr) arr.remove(3) # Removes the first occurrence of 3 print(arr) val = arr.pop(1) # Remove and return the element at index 1 print(val) print(arr) Explanation: We added 4 to the end of the array using append().We used insert() to add 10 at index 2, shifting the elements after it.remove() removes the first occurrence of the value 3 from the array.The pop() method removes and returns the element at index 1. In this case, it removes 2 and the array is updated accordingly. Create Quiz Comment A anuragtriarna Follow 0 Improve A anuragtriarna Follow 0 Improve Article Tags : Python Python Programs Python-array Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like