Table of contents
1.
Introduction
2.
Declare an Array in Python Using Lists
2.1.
Syntax for Declaring a List
2.2.
Example of Using Lists as Arrays
2.3.
Python
2.4.
Key Points in the Program
3.
Declare an Array Using the Array Module in Python
3.1.
Syntax for Using the Array Module
3.2.
Example of Using the Array Module
3.3.
Python
3.4.
Key Points in This Example
4.
Declare an Array Using NumPy Module in Python
4.1.
Syntax for Using NumPy Arrays
5.
Example of Using NumPy Module
5.1.
Python
5.2.
Key Points in This Example
6.
Frequently Asked Questions
6.1.
How Do You Declare an Array in Python?
6.2.
What Does array() Mean in Python?
6.3.
How Do You Define an Array in Code?
7.
Conclusion
Last Updated: Aug 11, 2025
Easy

How to Declare Array in Python

Author Riya Singh
0 upvote

Introduction

Arrays are an essential data structure in programming, used to store collections of elements in an organized manner. In Python, arrays allow developers to work with multiple values efficiently, making them ideal for tasks like data manipulation, mathematical computations, and implementing algorithms. Unlike some other languages, Python doesn’t have a built-in array type but provides various tools like the array module and libraries such as NumPy to create and manage arrays. In this blog, we’ll explore how to declare arrays in Python.

How to Declare Array in Python

Declare an Array in Python Using Lists

In Python, the most common way to create an array-like structure is by using lists. Lists are versatile & can hold any type of data, including integers, strings, or even other lists. They are dynamic, meaning you can add or remove items without specifying the size ahead of time.

Syntax for Declaring a List

To declare a list in Python, you simply assign items enclosed in square brackets to a variable. For example:

my_list = [1, 2, 3, 4, 5]

Example of Using Lists as Arrays

This example simulates managing a simple inventory list for a store, showing how dynamic and flexible lists can be when used as arrays : 

  • Python

Python

# Declare the initial inventory list of items in a store
inventory = ["apples", "bananas", "carrots"]

# Print the initial inventory
print("Initial inventory:", inventory)

# Adding more items to the inventory
inventory.append("dates") # Add a new item at the end
inventory.extend(["eggplants", "figs"]) # Add multiple items at the end

# Removing an item from the inventory
inventory.remove("carrots") # Remove a specific item by name

# Updating an item within the inventory
inventory[1] = "blueberries" # Change 'bananas' to 'blueberries'

# Displaying the updated inventory
print("Updated inventory:", inventory)

# Accessing items from the inventory
print("First item in the inventory:", inventory[0]) # Access the first item

# Example of using a loop to display all items
print("Complete inventory list:")
for item in inventory:
print(item)
You can also try this code with Online Python Compiler
Run Code

Output

Initial inventory: ['apples', 'bananas', 'carrots']
Updated inventory: ['apples', 'blueberries', 'dates', 'eggplants', 'figs']
First item in the inventory: apples
Complete inventory list:
apples
blueberries
dates
eggplants
figs

Key Points in the Program

  1. Initializing the List: The inventory list is initially created with a few items.
     
  2. Adding Items: New items are added to the list using append() and extend(), demonstrating how lists can grow dynamically.
     
  3. Modifying the List: The example shows how to update and remove items from the list.
     
  4. Accessing Items: Items in the list are accessed by their index, similar to how you would with a traditional array.
     
  5. Looping through the List: A for-loop is used to iterate through the list and print each item, showing the list's iterable nature.

Declare an Array Using the Array Module in Python

While lists in Python are powerful & flexible, sometimes you might need a more efficient way to store data of a single type. This is where the array module comes into play. The array module allows you to create arrays more efficiently than lists when you're dealing with large amounts of numeric data.

Syntax for Using the Array Module

To use arrays from the array module, you first need to import the module & then create an array with a specific type code. These type codes specify the type of data the array will hold. For example, 'i' is a type code for integers.

Here’s how you can declare an array using the array module:

import array
# Create an array of integer type
numbers = array.array('i', [1, 2, 3, 4, 5])

Example of Using the Array Module

Let's look at a simple example where we manipulate numerical data using arrays:

  • Python

Python

import array

# Declare an array of float numbers
floats = array.array('f', [1.0, 2.5, 3.5, 4.75, 5.25])

# Print the original array
print("Original float array:", floats)

# Append a new element to the array
floats.append(6.5)
print("Array after adding 6.5:", floats)

# Update an element in the array
floats[2] = 3.75
print("Array after updating the third element:", floats)

# Accessing elements from the array
print("First element of the array:", floats[0])

# Iterate over the array elements
print("All elements in the float array:")
for number in floats:
print(number)
You can also try this code with Online Python Compiler
Run Code

Output

Original float array: array('f', [1.0, 2.5, 3.5, 4.75, 5.25])
Array after adding 6.5: array('f', [1.0, 2.5, 3.5, 4.75, 5.25, 6.5])
Array after updating the third element: array('f', [1.0, 2.5, 3.75, 4.75, 5.25, 6.5])
First element of the array: 1.0
All elements in the float array:
1.0
2.5
3.75
4.75
5.25
6.5

Key Points in This Example

  1. Importing the Module: The array module is imported to access its functionalities.
     
  2. Array Creation: An array of floats is created with specific values.
     
  3. Modifying the Array: Elements are added and updated, showing how to manipulate the array.
     
  4. Accessing & Iterating: Elements are accessed by their index, and a loop is used to iterate through all elements.
     

Note -: Arrays created with the array module are more space-efficient than lists when dealing with data of a single type, making them particularly useful for data analysis and scientific computing where memory and performance are crucial.

Declare an Array Using NumPy Module in Python

NumPy is a powerful library in Python that supports large, multi-dimensional arrays & matrices, along with a large collection of high-level mathematical functions to operate on these arrays. If you're working with numerical data on a large scale, NumPy is the go-to library because of its efficiency & functionality.

Syntax for Using NumPy Arrays

To use NumPy arrays, you need to install the NumPy library if it’s not already installed, using pip install numpy. Then, you can import the library & create arrays.

Here's the basic syntax for creating a NumPy array:

import numpy as np
# Create a NumPy array with integers
num_array = np.array([1, 2, 3, 4, 5])

Example of Using NumPy Module

Let's create a more detailed example where we perform some operations on a NumPy array. This example will include creating the array, modifying it, and performing a simple mathematical operation.

  • Python

Python

import numpy as np

# Creating a NumPy array from a Python list
data = np.array([10, 20, 30, 40, 50])

# Display the original array
print("Original Array:", data)

# Adding a constant value to each array element
incremented_data = data + 5
print("Array after adding 5 to each element:", incremented_data)

# Multiplying each element by 2
doubled_data = data * 2
print("Array after doubling each element:", doubled_data)

# Accessing elements and slicing
print("Second element of the array:", data[1])
print("First three elements of the array:", data[:3])

# Using a mathematical function from NumPy
mean_value = np.mean(data)
print("Mean of the array elements:", mean_value)
You can also try this code with Online Python Compiler
Run Code

Output

Original Array: [10 20 30 40 50]
Array after adding 5 to each element: [15 25 35 45 55]
Array after doubling each element: [ 20  40  60  80 100]
Second element of the array: 20
First three elements of the array: [10 20 30]
Mean of the array elements: 30.0

Key Points in This Example

  1. Array Creation: A NumPy array is created from a regular Python list.
     
  2. Mathematical Operations: Simple arithmetic operations like addition & multiplication are performed on the array elements.
     
  3. Slicing & Accessing: Elements are accessed individually & in slices to demonstrate array indexing.
     
  4. Using NumPy Functions: The mean of the array is calculated using NumPy's built-in function, showcasing how the library can simplify complex operations.
     

Note -: NumPy arrays provide a significant performance boost & additional functionalities for numerical computations compared to standard Python lists, making them essential for scientific computing.

Frequently Asked Questions

How Do You Declare an Array in Python?

You can declare an array in Python using the array module or libraries like NumPy, e.g., array.array('i', [1, 2, 3]).

What Does array() Mean in Python?

The array() function from the array module creates arrays with type-specific elements, providing efficient storage and operations for homogeneous data.

How Do You Define an Array in Code?

Example using the array module:

import array

arr = array.array('i', [1, 2, 3])  # Integer array

This creates an array of integers.

Conclusion

In this article, we have learned about different methods to declare arrays in Python using lists, the array module, and the NumPy library. Each method has its specific uses and benefits, from the simplicity and flexibility of lists to the performance and functionality enhancements offered by NumPy. 

Live masterclass