To print arrays in Python, you can use the print() function directly for simple output or implement loops for formatted display. This guide covers both approaches for 1D and 2D arrays with practical examples.
Array printing in Python hits different when you’re debugging at 3 AM.
What starts as a simple print() statement can turn into an entire formatting philosophy.
I’ve been down this rabbit hole. Multiple times.
What Are Python Arrays?
First things first, there are no arrays in Python.
You get lists and NumPy arrays.
- Lists are Python’s default – flexible, forgiving, and deceptively simple.
- NumPy arrays want type consistency. They reward you with performance.
This distinction becomes critical when you’re trying to make sense of your output at 2 AM.
Printing Python Arrays Using Lists
Directly Printing Arrays with print()
Sometimes the simplest approach is the right one:
# Simple 1D array
numbers = [2, 4, 5, 7, 9]
print("Array:", numbers)
# 2D array (list of lists)
matrix = [[1, 2], [3, 4]]
print("2D Array:", matrix)
Output:
Array: [2, 4, 5, 7, 9]
2D Array: [[1, 2], [3, 4]]
Quick. Dirty. Perfect for those “what the hell is in this variable?” moments.
But we’re not always debugging. Sometimes we need our data to actually make sense to humans.
Custom Formatted Printing with Loops
This is where you separate the juniors from the seniors. The ability to format output properly:
# 1D array with custom spacing
data = [2, 4, 5, 7, 9]
print("Values: ", end="")
for value in data:
print(value, end=" ")
print() # New line
# 2D array as a grid
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("Grid format:")
for row in grid:
for item in row:
print(f"{item:2}", end=" ") # Format with width 2
print() # New line after each row
Output:
Values: 2 4 5 7 9
Grid format:
1 2 3
4 5 6
7 8 9
Now we’re talking. Readable output. Aligned columns. The kind of formatting that won’t make your eyes bleed during code review.
Printing Python NumPy Arrays
Basic NumPy Array Printing
NumPy doesn’t mess around with formatting:
import numpy as np
# 1D NumPy array
vector = np.array([1, 2, 3, 4])
print("Vector:", vector)
# 2D NumPy array
matrix = np.array([[21, 43], [22, 55], [53, 86]])
print("Matrix:\n", matrix)
Output:
Vector: [1 2 3 4]
Matrix:
[[21 43]
[22 55]
[53 86]]
Notice the difference? No commas. Automatic alignment. NumPy gets it.
Advanced NumPy Formatting and Printing
NumPy’s formatting options are… extensive. Sometimes overwhelmingly so:
import numpy as np
# Control decimal places
floats = np.array([3.14159, 2.71828, 1.41421])
np.set_printoptions(precision=3)
print("Formatted floats:", floats)
# Large arrays with ellipsis
large_array = np.arange(100)
np.set_printoptions(threshold=10)
print("Large array:", large_array)
# Reset to default
np.set_printoptions(edgeitems=3, threshold=1000, precision=8)
Output:
Formatted floats: [3.142 2.718 1.414]
Large array: [ 0 1 2 ... 97 98 99]
Powerful when you need it. Overkill when you don’t.
When Your Output Actually Matters
Let’s face it – most of us aren’t printing arrays for fun. We’re trying to make sense of data:
import numpy as np
# Sales data by region and quarter
sales_data = np.array([
[45000, 52000, 48000, 51000], # North
[38000, 41000, 43000, 46000], # South
[55000, 58000, 54000, 59000], # East
[42000, 44000, 47000, 49000] # West
])
regions = ['North', 'South', 'East', 'West']
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
# Print formatted table
print("Sales by Region and Quarter")
print("-" * 40)
print("Region ", end="")
for q in quarters:
print(f"{q:>8}", end="")
print()
print("-" * 40)
for i, region in enumerate(regions):
print(f"{region:<8}", end="")
for sale in sales_data[i]:
print(f"{sale:8,}", end="")
print()
Output:
Sales by Region and Quarter
----------------------------------------
Region Q1 Q2 Q3 Q4
----------------------------------------
North 45,000 52,000 48,000 51,000
South 38,000 41,000 43,000 46,000
East 55,000 58,000 54,000 59,000
West 42,000 44,000 47,000 49,000
This is what clean output looks like.
Scientific Computing Array Printing
When precision matters more than aesthetics. Let’s display a correlation matrix:
import numpy as np
# Correlation matrix
correlation = np.array([
[1.000, 0.812, 0.543],
[0.812, 1.000, 0.721],
[0.543, 0.721, 1.000]
])
variables = ['Temperature', 'Pressure', 'Volume']
print("Correlation Matrix")
print("-" * 50)
print(f"{'':12}", end="")
for var in variables:
print(f"{var[:11]:>12}", end="")
print()
for i, var in enumerate(variables):
print(f"{var[:11]:<12}", end="")
for j, value in enumerate(correlation[i]):
print(f"{value:12.3f}", end="")
print()
Output:
Correlation Matrix
--------------------------------------------------
Temperature Pressure Volume
Temperature 1.000 0.812 0.543
Pressure 0.812 1.000 0.721
Volume 0.543 0.721 1.000
Clean. Professional. The kind of output that doesn’t make your colleagues question your competence.
Best Practices and Reality Checks
Choosing the Right Array Printing Method
Your approach should fit your context:
- Debugging? print() and move on
- Code review coming up? Format that shit properly
- Scientific paper? NumPy formatting is your friend
- Teaching others? Clear examples with clean output
Conclusion
Array printing in Python isn’t hard. But the difference between amateur hour and professional output is in these details.
Start simple. Use print() when debugging. Format properly when sharing code. Use NumPy when you need precision.
The best approach? Whatever makes your output understandable to the poor soul reading it later.
Because that poor soul is usually future you.