Introduction to Python Arrays
Python Arrays
The array
module provides a way to create arrays, which are similar to lists but have a fixed type for their elements.
Arrays can be more efficient in terms of memory usage and performance when dealing with large sequences of homogeneous data.
First, you need to import the array
module:
import array
To create an array, you use the array.array()
function and specify the type code for the array elements.
The type code determines the type and size of each element in the array.
Here are some commonly used type codes:
'b'
: signed integer of size 1 byte'B'
: unsigned integer of size 1 byte'i'
: signed integer of size 2 bytes'I'
: unsigned integer of size 2 bytes'l'
: signed integer of size 4 bytes'L'
: unsigned integer of size 4 bytes'f'
: floating-point of size 4 bytes'd'
: floating-point of size 8 bytes
Here's an example that creates an array of integers:
import array
my_array = array.array('i', [1, 2, 3, 4, 5])
In this example:
- We create an array of integers using the
'i'
type code. - The second argument
[1, 2, 3, 4, 5]
is a list of initial values for the array.
Accessing array element
You can access and manipulate array elements similar to how you work with lists.
As an example:
import array
my_array = array.array('i', [1, 2, 3, 4, 5])
print(my_array[0]) # Output: 1
my_array[2] = 10
print(my_array) # Output: array('i', [1, 2, 10, 4, 5])
In this example:
- We access the first element of the array using indexing (
my_array[0]
) and modify the third element by assigning a new value (my_array[2] = 10
).
Array Methods
You can also perform various operations on arrays, such as appending elements, removing elements, getting the length, and more. The array module provides methods for these operations.
As an example:
import array
my_array = array.array('i', [1, 2, 3, 4, 5])
my_array.append(6)
print(my_array) # Output: array('i', [1, 2, 3, 4, 5, 6])
my_array.pop(2)
print(my_array) # Output: array('i', [1, 2, 4, 5, 6])
length = len(my_array)
print(length) # Output: 5
In this example:
- We append an element to the array using the
append()
method, remove an element at index 2 using thepop()
method, and get the length of the array using thelen()
function.
Array Slicing
You can use array slicing to extract a portion of an array. Slicing is done using the colon (:
) notation.
As an example:
import array
my_array = array.array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
sliced_array = my_array[2:5]
print(sliced_array) # Output: array('i', [3, 4, 5])
In this example:
- The slice
my_array[2:5]
extracts elements from index 2 to index 4 (exclusive) and creates a new array.
Concatenation
You can concatenate two arrays using the + operator. The result is a new array that contains the elements of both arrays.
As an example:
import array
array1 = array.array('i', [1, 2, 3])
array2 = array.array('i', [4, 5, 6])
concatenated_array = array1 + array2
print(concatenated_array) # Output: array('i', [1, 2, 3, 4, 5, 6])
In this example:
- The arrays
array1
andarray2
are concatenated using the + operator, resulting in a new array.
Iteration
You can iterate over the elements of an array using a loop, such as a for
loop.
As an example:
import array
my_array = array.array('i', [1, 2, 3, 4, 5])
for element in my_array:
print(element)
Array Methods
The array
module provides several useful methods for working with arrays.
Here are a few examples:
insert(index, value):
Inserts an element at the specified index.remove(value):
Removes the first occurrence of the specified value from the array.index(value):
Returns the index of the first occurrence of the specified value.count(value):
Returns the number of occurrences of the specified value in the array.reverse():
Reverses the order of the elements in the array.extend(iterable):
Appends elements from the iterable to the end of the array. You can use these methods to manipulate and modify arrays according to your needs.