Python - Update in Array Last Updated : 08 Dec, 2024 Summarize Comments Improve Suggest changes Share 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. Comment More infoAdvertise with us Next Article How to Update a Dictionary in Python A anuragtriarna Follow Improve Article Tags : Python Python Programs Python-array Practice Tags : python Similar Reads How to Update a Dictionary in Python This article explores updating dictionaries in Python, where keys of any type map to values, focusing on various methods to modify key-value pairs in this versatile data structure. Update a Dictionary in PythonBelow, are the approaches to Update a Dictionary in Python: Using with Direct assignmentUs 3 min read Python Update Set Items Python sets are a powerful data structure for storing unique, unordered items. While sets do not support direct item updating due to their unordered nature, there are several methods to update the contents of a set by adding or removing items. This article explores the different techniques for updat 2 min read Python Update Tuples Tuples in Python are often introduced as immutable objects, meaning once a tuple is created, its contents cannot be modified or updated directly. However, there are techniques that allow us to "update" a tuple if we need to change its contents. This article will explore these methods and show you ho 3 min read Update a Dictionary in Python using For Loop Updating dictionaries in Python is a common task in programming, and it can be accomplished using various approaches. In this article, we will explore different methods to update a dictionary using a for loop. Update a Dictionary in Python Using For Loop in PythonBelow are some of the approaches by 3 min read Python Update Dictionary Value by Key A Dictionary in Python is an unordered collection of key-value pairs. Each key must be unique, and you can use various data types for both keys and values. Dictionaries are enclosed in curly braces {}, and the key-value pairs are separated by colons. Python dictionaries are mutable, meaning you can 3 min read Variable Operations Dictionary Update - Python The task of performing variable operations in a dictionary in Python involves updating the values associated with specific keys based on certain operations. For example, consider a scenario where x = 6 and y = 7. The goal could be to store the sum of x and y under the key 'Gfg' and the product of x 3 min read Like