In Python, there are several data structures available for storing collections of values, including lists, tuples, and dictionaries. Arrays are another type of collection that is commonly used in other programming languages, but they are not as commonly used in Python.
Python has built-in support for arrays through the array
module, which provides an efficient way to store and manipulate homogeneous collections of numerical data. Homogeneous means that all the elements of an array must be of the same type.
To use the array
module, you first need to import it:
import array
You can then create an array by specifying the type of data it will store and providing a sequence of values to initialize it:
my_array = array.array('i', [1, 2, 3, 4, 5])
In this example, my_array
is an array of integers ('i'
), initialized with the values 1, 2, 3, 4, and 5.
Once you have created an array, you can access its elements using indexing, just like you would with a list:
print(my_array[0]) # prints 1 print(my_array[2]) # prints 3
You can also modify the values of an array by assigning to its elements:
my_array[0] = 10 print(my_array[0]) # prints 10
The array
module provides several other methods for manipulating arrays, including appending and extending arrays, slicing, and sorting. You can find more information on these methods and other details about using arrays in the Python documentation.
Array operations:
Arrays support a variety of operations that allow you to manipulate and work with their elements. Here are some of the most common operations:
- Accessing Elements: You can access the elements of an array using indexing, just like with a list. For example,
my_array[0]
would return the first element of the array. - Modifying Elements: You can modify the values of an array by assigning new values to its elements. For example,
my_array[0] = 5
would change the value of the first element to 5. - Slicing: You can create a new array by slicing a portion of an existing array. For example,
new_array = my_array[1:3]
would create a new array containing the second and third elements ofmy_array
. - Appending: You can append new elements to an array using the
append()
method. For example,my_array.append(6)
would add the value 6 to the end of the array. - Extending: You can extend an array by appending the elements of another array using the
extend()
method. For example,my_array.extend([6, 7, 8])
would add the values 6, 7, and 8 to the end of the array. - Deleting Elements: You can remove elements from an array using the
remove()
method or thedel
keyword. For example,my_array.remove(5)
would remove the first occurrence of the value 5 from the array, whiledel my_array[0]
would delete the first element of the array. - Sorting: You can sort the elements of an array using the
sort()
method. For example,my_array.sort()
would sort the elements of the array in ascending order. - Reversing: You can reverse the order of the elements in an array using the
reverse()
method. For example,my_array.reverse()
would reverse the order of the elements in the array.
These are just some of the operations you can perform on arrays in Python. The array
module also provides other methods for manipulating arrays, so be sure to check the documentation for more information.