Update Array Python

In this Python tutorial, you will understand update array Python where you will update the array items or value with new value.

To implement the functionality of updating user profiles that were stored in the list or call it an array, I created a custom function for my Python application, which updates the user profile with new user details.

So in this tutorial, I have explained with simple examples how you can update any array or list

Update Array Python

Before updating the array in Python, I want to tell you that there is no array in Python. Instead, it has an alternative called a list, which stores a collection of items of different datatypes.

Also, a list is mutable like an array, which means you can add, modify, and delete the list item.

So whenever I say array, consider it as if I am talking about a list in Python. First, I want to show you how to create an array in Python if you don’t know.

In Python, the square bracket ‘[ ]’ is used to create an array; within the square bracket, items are specified, and each item is separated by a comma.

For example, [4, 5, 7] is a list of integers that contains 3 items or elements of type integer. Look at each number or element separated by a comma. This list contains the homogenous types of elements; you can also specify the heterogenous type, like one string, another integer, etc.

Now that you are familiar with an array or list and how to create it, let’s examine a few approaches to updating the array in Python.

READ:  PyTorch Softmax [Complete tutorial]

Python Array Update

To update the array or list in Python, you can use the concept of indexing; if you don’t know, each item in the list has an index. For example, the first item will have an index of 0, the second will have 1, and so on for all other items in the list.

First, create a list that is going to contain the list of users as shown below.

users = ["Joe", "Logan", "Juan", "Albert", "Willie"]

print("Array before updating", users)

If you want to access the first item of the list, you can use indexing here: users[0] represent the first item, “Joe” and users[1] represent the second item, “Logan”. Similarly, you can access all the items on the list.

Suppose you need to update the name of the first user in the list from “Joe” to “Joe Smith.” To update the array or list, you can use indexing, as shown below.

users[0]="Joe Smith"

print("Array after updating", users)
Update Array Python using Indexing

The look array is updated, which means the user name is updated from “Joe” to “Joe Smit” using indexing like this: users[0]=”Joe Smith” But here is a problem: you will need to write complete code to update any array value each time.

It would be better to wrap the whole concept within a function. Let’s create a general function that can take any list and update its value based on the index.

You can create an update function by providing the list, index value, and new value that you want to update. For example, use the code below, which creates a function named update_list.

def update_list(list, index, new_value):
  list[index] = new_value
  return list

The above function takes three parameters a list, index and new_value. For example, you want to update the user name in the list ‘users’ at index 3 with a new value as “Albert Einstein”. Look at the code below.

update_list(users, 3, "Albert Einstein")
Update Array in Python using Function

Look at the output; the value at index 3 is updated from “Albert” to “Albert Einstein.” So, you need to call the function and provide the required parameter to update the array or list.

READ:  Python remove first character from String [6 Methods]

Similarly, you can update any items on the list using their index. This is how to update an array in Python.

Conclusion

In this Python tutorial, you covered update array Python where you update the array or list containing a user name with the new user name. You learned how to create your own custom function to update any list with the specified values.

You may like to read: