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 update an array in Python?
Now, let us see how to update 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:
Operation | Description | Example |
---|---|---|
Append | Adds an element to the end of the array | cities.append("Dallas") |
Insert | Inserts an element at a specified position | cities.insert(2, "Denver") |
Remove | Removes the first occurrence of an element | cities.remove("Chicago") |
Pop | Removes and returns the element at the given index | cities.pop(3) |
Index | Returns the index of the first occurrence of an element | cities.index("Houston") |
Count | Counts the number of occurrences of an element | cities.count("Phoenix") |
Sort | Sorts the array | cities.sort() |
Reverse | Reverses the array | cities.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:
- Split a String into an Array in Python
- Convert NumPy Array to List of Lists in Python
- convert numpy array to list of strings in Python
- How to Reverse NumPy array in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.