How to Convert a List to an Array in Python?

In this tutorial, I will explain how to convert a list to an array in Python. As a data scientist working with large datasets in the USA, I often need to convert Python lists to NumPy arrays for more efficient computation and analysis. I will explain various ways to achieve this task.

Convert a List to an Array using NumPy in Python

The simplest way to convert a Python list to a NumPy array is by using the numpy.array() function. Here’s an example:

import numpy as np

# Create a Python list
us_cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]

# Convert the list to a NumPy array
us_cities_array = np.array(us_cities)

print(us_cities_array)

Output:

['New York' 'Los Angeles' 'Chicago' 'Houston' 'Phoenix']

You can see the executed example code in the below screenshot.

Convert a List to an Array in Python

In this example, we first import the NumPy library. Then, we create a Python list us_cities containing the names of some major cities in the USA. Finally, we use np.array() to convert the list to a NumPy array and store it in the variable us_cities_array.

Read How to Convert a Dictionary to an Array in Python

Convert a List of Numbers to an Array in Python

Converting a list of numbers to a NumPy array follows the same process. Here’s an example:

import numpy as np

# Create a Python list of numbers
us_population = [8336817, 3898747, 2746388, 2320268, 1680992]

# Convert the list to a NumPy array
us_population_array = np.array(us_population)

print(us_population_array)

Output:

[8336817 3898747 2746388 2320268 1680992]

You can see the executed example code in the below screenshot.

How to Convert a List to an Array in Python

In this case, we create a list

containing the population figures for the corresponding cities. We then use np.array() it to convert the list to a NumPy array.

Read How to Read a Binary File into a Byte Array in Python

Benefits of Using Python NumPy Arrays

Now that we’ve converted our lists to NumPy arrays, let’s explore some of the benefits:

1. Efficient Numerical Operations

NumPy arrays allow you to perform element-wise operations efficiently. For example, let’s calculate the square of each population value:

squared_population = us_population_array ** 2

print(squared_population)

Output:

[69502467142489 15200138274009  7541849413344  5383643525824  2827734310464]

You can see the executed example code in the below screenshot.

Convert a List to an Array in Python Efficient Numerical Operations

With NumPy arrays, you can perform mathematical operations directly on the array without needing loops or list comprehensions.

Read How to Create Arrays in Python

2. Slicing and Indexing

NumPy arrays support slicing and indexing, making accessing specific elements or subsets of the array easy. For example:

# Access the first three elements
first_three_cities = us_cities_array[:3]
print(first_three_cities)

# Access the last two population values
last_two_populations = us_population_array[-2:]
print(last_two_populations)

Output:

['New York' 'Los Angeles' 'Chicago']
[2320268 1680992]

Slicing allows you to extract portions of the array efficiently, which is particularly useful when working with large datasets.

Read How to Find the Maximum Value in Python Using the max() Function

3. Broadcasting

NumPy arrays support broadcasting, which enables you to perform operations between arrays of different shapes and sizes. This can greatly simplify your code and improve performance. For example:

# Create an array of ones with the same length as us_population_array
ones_array = np.ones(len(us_population_array))

# Multiply us_population_array by 1.1 (10% increase)
increased_population = us_population_array * 1.1

print(increased_population)

Output:

[9170498.7 4288621.7 3021026.8 2552294.8 1849091.2]

In this example, we create an array of ones with the same length as us_population_array using np.ones(). We then multiply us_population_array by 1.1 to simulate a 10% increase in population. NumPy automatically broadcasts the scalar value 1.1 to match the shape of us_population_array.

Read How to Find the Index of an Element in an Array in Python

Conclusion

In this tutorial, I have explained how to convert a list to an array in Python. I explained various methods to achieve this task using NumPy, converting a list of numbers to an array in Python, and some benefits of using NumPy, you can perform efficient numerical operations, slicing, indexing, and broadcasting, leading to faster and more streamlined data manipulation and analysis.

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.