top of page

Python Arrays

An array is a data structure that can store a fixed-size sequential collection of elements of the same type. In Python, this functionality is provided by the list type, but there’s also a module called array which provides more functionality.

Creating Arrays

In Python, we can create an array using the array module or just using lists. Here’s an example of creating an array with the array module:

Here, 'd' is a type code indicating the type of the array elements. In this case, it's a double precision float.

If you prefer to use lists, it's as simple as:

Accessing Array Elements

You can access elements of an array similar to how you access elements of a list. Just use the index of the element.

Remember, in Python, indexing starts from 0.

Modifying Array Elements

Modifying an element of an array is also similar to how you do it in a list.

Screenshot 2023-06-02 at 11.38_edited.jp

Array Slicing

Python provides a feature called slicing to access a range of elements in an array.

​

Screenshot 2023-06-02 at 12.40_edited.jp

Here, the output array includes the element at the start index, 1, and excludes the element at the end index, 4.

Looping Through an Array

You can use a for loop to iterate through all the elements in an array.

​

Screenshot 2023-06-02 at 1.16_edited.jpg

Array Methods

Python arrays (or lists) have many useful methods, such as append to add an element at the end, insert to add an element at a specific position, remove to remove the first occurrence of a specific element, and many more.

we just covered the basics of Python arrays. There's much more to learn, including multi-dimensional arrays and array functions in the NumPy module, which is a powerful library for numerical computations in Python.

bottom of page