How to Print an Array in Python?

In this tutorial, I will explain how to print an array in Python. Someone asked me this doubt during a Python webinar and this was the topic of discussion. So I decided to write an article on this. We will explore different methods to print an array in Python with examples and screenshots of executed example code.

Array in Python

An array is a data structure that can hold more than one value at a time. It is similar to a list but with some differences. Arrays in Python are useful when you need to store a collection of data, such as a list of cities in the USA or a series of numbers.

Read ValueError: Can Only Convert an Array of Size 1 to a Python Scalar [How to Fix]

Create an Array in Python

To create an array in Python, you need to import the array module. Here’s how you can do it:

import array as arr

# Creating an array of integers
numbers = arr.array('i', [1, 2, 3, 4, 5])

In the above example, 'i' is the type code for integers. You can also create arrays of different data types, such as floats or characters.

Check out How to Create an Array from 1 to N in Python

Print an Array in Python

Python provides various methods to print an array in Python.

Method 1. Use print()method

Printing an array in Python is simple. You can use the built-in print() function to display the contents of an array. Here’s an example:

Let’s say you have an array of populations of different states in the USA. Here’s how you can print it:

import array as arr

# Creating an array of state populations
populations = arr.array('i', [39538223, 29145505, 21538187, 19376797, 12812508])

# Printing the array
print("State populations:", populations)

Output:

State populations: array('i', [39538223, 29145505, 21538187, 19376797, 12812508])

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

Print an Array in Python

Read How to Write an Array to a File in Python

Method 2. Access Python Array Elements Using the Index

You can access elements in an array using their index. Here’s an example:

# Creating a list of state abbreviations
state_abbreviations = ['CA', 'TX', 'FL', 'NY', 'PA']

# Accessing the first element
print("First state abbreviation:", state_abbreviations[0])

# Accessing the last element
print("Last state abbreviation:", state_abbreviations[-1])

Output:

First state abbreviation: CA
Last state abbreviation: PA

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

How to Print an Array in Python

Check out How to Find the Index of the Maximum Value in an Array Using Python

Method 3. Looping Through an Array in Python

You can use loops to iterate through the elements of an array. Here’s an example using a for loop:

# Creating a list of state capitals
state_capitals = ['Sacramento', 'Austin', 'Tallahassee', 'Albany', 'Harrisburg']

# Looping through the list
for capital in state_capitals:
    print("State capital:", capital)

Output:

State capital: Sacramento
State capital: Austin
State capital: Tallahassee
State capital: Albany
State capital: Harrisburg

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

Print an Array in Python Looping Through an Array

Read How to Reverse an Array in Python

Advanced Printing Techniques

Python supports some advanced techniques to achieve this task.

Method 1. Print with Indexes

Sometimes, you might want to print the index along with the array elements. Here’s how you can do it:

import array as arr

# Creating an array of state nicknames
state_nicknames = arr.array('u', ['The Golden State', 'The Lone Star State', 'The Sunshine State', 'The Empire State', 'The Keystone State'])

# Printing elements with indexes
for index, nickname in enumerate(state_nicknames):
    print(f"Index {index}: {nickname}")

Method 2. Pretty Print

For a more readable output, you can use the pprint module from the Python standard library:

import array as arr
from pprint import pprint

# Creating an array of state birds
state_birds = arr.array('u', ['California Quail', 'Northern Mockingbird', 'Northern Mockingbird', 'Eastern Bluebird', 'Ruffed Grouse'])

# Pretty printing the array
pprint(state_birds.tolist())

Check out How to Read a File into an Array in Python

Common Issues and Troubleshoot

1. TypeError: ‘array’ Object is Not Callable

This error occurs if you try to call an array object as if it were a function. Ensure you are not using parentheses () after the array name. Use square brackets [] for indexing.

2. ValueError: Bad Typecode

This error happens when you use an incorrect type code.

Check out How to Initialize an Array in Python

Conclusion

In this tutorial, I have explained how to print an array in Python. I explained how to create an array in Python and how to print an array by various methods using print() method, accessing array elements using the index, and looping through an array and some advanced techniques like printing with indexes, and pretty printing, We also discussed some common issues and troubleshooting that may be TypeError and ValueError.

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.