An array is a data structure that stores a collection of items of the same data type. In Python, arrays are not a built-in data type, but can be created using the array module.
Creating an Array
To create an array in Python, the array module must first be imported.
The array() function is then used to create an array by passing in the data type of the items, and an optional list or tuple of items.
Here is an example of creating an array of integers:
import array # Create an array of integers int_array = array.array('i', [1, 2, 3, 4, 5]) print(int_array) # Output: array('i', [1, 2, 3, 4, 5])
In this example, the first argument passed to the array() function is the data type code ‘i’, which stands for integer.
The second argument is a list of integers that will be stored in the array.
It is also possible to create an array without initial items, and then add items to it later using the append() or extend() methods.
import array # Create an empty array of floats float_array = array.array('f') # Add items to the array float_array.append(1.5) float_array.append(2.5) float_array.append(3.5) print(float_array) # Output: array('f', [1.5, 2.5, 3.5])
Accessing Array Elements
Items in an array can be accessed using their index, just like in a list or tuple.
The first item has an index of 0, the second has an index of 1, and so on.
Here is an example of accessing items in an array:
import array int_array = array.array('i', [1, 2, 3, 4, 5]) # Access the first item print(int_array) # Output: 1 # Access the third item print(int_array) # Output: 3
Modifying Array Elements
Individual items in an array can be modified by assigning a new value to a specific index.
import array int_array = array.array('i', [1, 2, 3, 4, 5]) # Modify the second item int_array = 9 print(int_array) # Output: array('i', [1, 9, 3, 4, 5])
Deleting Array Elements
To delete an item from an array, the del keyword can be used along with the index of the item to be deleted.
import array int_array = array.array('i', [1, 2, 3, 4, 5]) # Delete the third item del int_array print(int_array) # Output: array('i', [1, 2, 4, 5])
The array class in Python provides several methods for working with arrays, such as:
- append() – adds an item to the end of the array
- extend() – adds multiple items to the end of the array
- insert() – inserts an item at a specific index
- remove() – removes the first occurrence of a specific item
- pop() – removes and returns the item at a specific index
- index() – returns the index of the first occurrence of a specific item
- count() – returns the number of occurrences of a specific item
- reverse() – reverses the order of the items in the array
Here is an example of using some of these methods:
import array int_array = array.array('i', [1, 2, 3, 4, 5]) # Append a new item int_array.append(6) # Extend the array with a list of new items int_array.extend([7, 8, 9]) # Insert a new item at index 2 int_array.insert(2, 10) # Remove the first occurrence of the item 4 int_array.remove(4) # Pop and return the item at index 4 popped_item = int_array.pop(4) print(int_array) # Output: array('i', [1, 2, 10, 3, 5, 6, 7, 8, 9]) print(popped_item) # Output: 5 # Find the index of the item 9 print(int_array.index(9)) # Output: 8 # Count the number of occurrences of the item 3 print(int_array.count(3)) # Output: 1 # Reverse the order of the items in the array int_array.reverse() print(int_array) # Output: array('i', [9, 8, 7, 6, 5, 3, 10, 2, 1])
Arrays in Python are faster and use less memory compared to lists, because they are implemented in C and do not require the overhead of Python’s data types.
They are also more efficient for certain operations, such as appending and popping items from the end of the array.
However, they have a fixed size, and resizing an array can be more time-consuming than resizing a list.
In general, arrays are a good choice when working with large amounts of homogeneous data and when performance is a concern.
However, if the size of the data is likely to change frequently, or if the data is heterogeneous, a list may be a better choice.
In conclusion, arrays in Python provide a way to store and work with homogeneous data, and can offer better performance than lists for certain operations.
The array module provides a variety of methods for manipulating arrays, and can be a useful tool when working with large amounts of data.