Python Array | Python Array Update [With Examples]

In this Python tutorial, we will discuss Python array with examples. I will also show you examples of how to update an array in Python. Then we will discuss, how to access array elements in Python.

What is an Array in Python?

An array in Python is a collection of elements, each identified by an index or a key. In Python, you can create an array using lists, or you can use the array module which provides an array data structure more efficiently than lists for certain operations. Arrays in Python are homogenous; that is, all the elements in an array must be of the same type.

Create an Array in Python

You can create an array in Python using a list like this:

cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]

Or, you can use the array module to define an array in Python like the below:

import array as arr

cities = arr.array('u', ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"])

In the second example, the ‘u’ denotes that the elements are Unicode characters.

How to access array elements in Python

Let us see, how to access array elements in Python. Accessing elements in a Python array is simple and straightforward. You can access any element by its index. The index starts at 0.

# Accessing the first element
first_city = cities[0]
print("The first city is:", first_city)

# Accessing the third element
third_city = cities[2]
print("The third city is:", third_city)

Once you run the code, you can see the output below:

The first city is: New York
The third city is: Chicago
how to access array elements in python
how to access array elements in python

How to update an array in Python?

Now, let us see how to update an array in Python.

READ:  How to Split a String into an Array in Python

You can update elements in an array by accessing the element using its index and assigning it a new value.

# Updating the second element
cities[1] = "San Francisco"

# Printing the updated array
print("Updated cities array:", cities)

Output:

Updated cities array: ["New York", "San Francisco", "Chicago", "Houston", "Phoenix"]

Basic Operations on Arrays

Here are some common operations that can be performed on arrays in Python:

OperationDescriptionExample
AppendAdds an element to the end of the arraycities.append("Dallas")
InsertInserts an element at a specified positioncities.insert(2, "Denver")
RemoveRemoves the first occurrence of an elementcities.remove("Chicago")
PopRemoves and returns the element at the given indexcities.pop(3)
IndexReturns the index of the first occurrence of an elementcities.index("Houston")
CountCounts the number of occurrences of an elementcities.count("Phoenix")
SortSorts the arraycities.sort()
ReverseReverses the arraycities.reverse()

Examples of Array Operations

Now let’s combine everything we’ve learned and create a small program using an array.

import array as arr

# Creating an array of city names
cities = arr.array('u', ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"])

# Accessing elements
print("First city:", cities[0])
print("Fourth city:", cities[3])

# Updating elements
cities[1] = "San Francisco"
print("Updated cities array:", cities)

# Basic operations
cities.append("Miami")
print("After appending Miami:", cities)

cities.insert(2, "Denver")
print("After inserting Denver at index 2:", cities)

cities.remove("Chicago")
print("After removing Chicago:", cities)

index_of_houston = cities.index("Houston")
print("Index of Houston:", index_of_houston)

Conclusion

In this Python tutorial. I have explained you Python array and the below things with examples:

  • Python array update
  • how to access array elements in Python

You may also like: