How to Initialize an Array in Python?

In this tutorial, I will explain how to initialize an array in Python. Recently in a project for a client in New York, I came across initializing arrays in Python, where the data in the project involves student records. Let’s explore the various methods to initialize arrays with examples and screenshots of executed code.

What are Arrays in Python?

Before we get into array initialization, let’s understand what Python arrays are. Arrays are a collection of elements of the same data type, such as integers, floats, or strings. Arrays are data structures that provide a way to store and access multiple values using a single variable name.

Check out How to Multiply an Array by a Scalar in Python

Create and Initialize Python Arrays

Let us see various methods to create and initialize Python arrays:

Method 1: Use the list() Constructor

One of the simplest ways to initialize an array in Python is by using the built-in list() Constructor. Here’s an example:

# Initialize an empty array
student_names = list()

# Add elements to the array
student_names.append("John")
student_names.append("Emily")
student_names.append("Michael")

print(student_names)  

Output:

['John', 'Emily', 'Michael']

I executed the above example code and added its screenshot below.

Initialize an Array in Python

In this example, we initialize an empty array called student_names using the list() constructor. We then use the append() method to add elements at the end of the array.

Read How to Check if an Array Contains a Value in Python

Method 2: Use Square Brackets []

Another common way to initialize an array in Python is by using square brackets []. Here’s an example:

# Initialize an array with initial values
employee_ids = [1001, 1002, 1003, 1004]

print(employee_ids)   

Output:

 [1001, 1002, 1003, 1004]

I executed the above example code and added its screenshot below.

How to Initialize an Array in Python

In this example, we initialize an array called employee_ids using square brackets and provide the initial values directly. Each value is separated by a comma.

Check out How to Count Occurrences in Python Arrays

Add Elements to an Existing Python Array

If you have an existing array and want to add more elements to it, you can use the append() method or the extend() method. Here’s an example:

# Initialize an array with initial values
product_prices = [9.99, 14.99, 19.99]

# Add a single element using append()
product_prices.append(24.99)

# Add multiple elements using extend()
product_prices.extend([29.99, 34.99])

print(product_prices)    

Output:

[9.99, 14.99, 19.99, 24.99, 29.99, 34.99]

I executed the above example code and added its screenshot below.

Initialize an Array in Python Adding Elements to an Existing Array

In this example, we initialize an array called product_prices with initial values. We then use the append() method to add a single element to the end of the array. To add multiple elements at once, we use the extend() method and pass a list of values.

Check out How to Reshape an Array in Python Using the NumPy Library

Method 3: Use the array Module

Python provides a built-in array module that allows you to create arrays of a specific data type. Here’s an example:

import array

# Initialize an array of integers
employee_ages = array.array('i', [25, 30, 35, 40])

print(employee_ages)  

Output:

array('i', [25, 30, 35, 40])

In this example, we import the array module and use the array() function to initialize an array called employee_ages. The first argument 'i' specifies the data type of the array, which in this case is an integer.

Read How to Remove the First Element from an Array in Python

Initialize Python Array with a Default Value

Sometimes, you may need to initialize an array with a default value for all elements. You can achieve this using a for loop and the range() function. Here’s an example:

# Initialize an array with a default value
inventory_count = [0] * 10

print(inventory_count) 

Output:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

In this example, we initialize an array inventory_count with a default value of 0 for all elements. The [0] * 10 syntax creates an array of length 10, where each element is initialized to 0.

Check out How to Transpose an Array in Python

Conclusion

In this tutorial, I explained various methods to initialize arrays in Python. I helped you to learn how to use the list() constructor, square brackets [], and the array module to create and initialize arrays. We also saw how to add elements to existing arrays using the append() and extend() methods, as well as initializing arrays with default values.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.