Last modified: Mar 25, 2026 By Alexander Williams

How to Create an Array in Python

Arrays are fundamental data structures. They store multiple items in a single variable.

In Python, you can create arrays in different ways. This guide will show you how.

What is a Python Array?

An array is a collection of items. All items must be of the same data type.

This makes arrays efficient for numerical computations. Python has built-in lists, which are more flexible.

For true arrays, you need to use specific modules. Understanding the key differences is crucial. You can learn more in our guide on Python Array vs List: Key Differences Explained.

Method 1: Using the Built-in `array` Module

Python's standard library includes an `array` module. It provides a basic array structure.

First, you must import it. Then you can create an array with a type code.


# Import the array module
import array

# Create an array of integers ('i' is the type code for signed integers)
my_array = array.array('i', [10, 20, 30, 40, 50])

print(my_array)
print(type(my_array))
    

array('i', [10, 20, 30, 40, 50])
<class 'array.array'>
    

The first argument is a type code. 'i' stands for signed integer. Other codes include 'f' for float and 'd' for double.

This module is memory efficient. But it offers fewer features than lists or NumPy.

Method 2: Using NumPy for Powerful Arrays

For scientific computing, NumPy is the standard. It provides a powerful `ndarray` object.

You must install NumPy first. Use `pip install numpy` in your terminal.


import numpy as np

# Create a NumPy array from a list
np_array = np.array([1, 2, 3, 4, 5])
print("NumPy Array:", np_array)
print("Data Type:", np_array.dtype)

# Create a 2-dimensional array
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print("\n2D Array:")
print(matrix)
    

NumPy Array: [1 2 3 4 5]
Data Type: int64

2D Array:
[[1 2 3]
 [4 5 6]]
    

NumPy arrays are fast and support vectorized operations. They are the backbone of data science in Python.

For a deeper look at setting them up, see our Python Array Implementation Guide.

Basic Operations on Python Arrays

Once created, you will want to work with your array. Common tasks include accessing elements and finding the size.

Accessing Elements (Indexing)

You access array elements using their index. Indexing starts at 0.


import array
arr = array.array('i', [100, 200, 300, 400])

# Access the first element
first_element = arr[0]
print("First Element:", first_element)

# Access the last element
last_element = arr[-1]
print("Last Element:", last_element)
    

First Element: 100
Last Element: 400
    

Indexing is a core skill. Our Python Array Indexing Guide for Beginners covers it in detail.

Finding the Length of an Array

Use the built-in len() function. It returns the number of elements.


import array
arr = array.array('f', [1.5, 2.5, 3.5])
array_length = len(arr)
print("Array Length:", array_length)
    

Array Length: 3
    

Knowing the size is essential for loops and memory management. For more on this, check Python Array Length: How to Find It.

Adding Elements to an Array

You can append a new element to the end. Use the append() method.


import array
arr = array.array('i', [1, 2, 3])
print("Original:", arr)

arr.append(4)
print("After Append:", arr)
    

Original: array('i', [1, 2, 3])
After Append: array('i', [1, 2, 3, 4])
    

The append() method modifies the array in place. It is a simple way to grow your array.

When to Use Which Method?

Choose the right tool for your task.

Use the built-in `array` module when you need basic, memory-efficient arrays of a single type. It's good for simple tasks.

Use NumPy arrays for any mathematical or scientific work. They are fast and have extensive functionality.

Use Python lists when you need a simple, flexible collection of different data types.

Common Pitfalls and Tips

Beginners often confuse lists and arrays. Remember, lists are built-in and hold any type.

Arrays (from `array` or NumPy) require uniform types. Trying to mix types will cause an error.

Always check your type codes in the `array` module. Using the wrong one leads to unexpected results.

For NumPy, ensure it is installed. An `ImportError` means you need to run `pip install numpy`.

Conclusion

Creating an array in Python is straightforward. You have two main paths.

Use the basic `array` module from the standard library. Or use the powerful NumPy package for advanced work.

Start with the method that fits your project's needs. Practice creating arrays, accessing elements, and using methods like append().

Arrays are a key step in your Python journey. They unlock efficient data handling and complex computations.