How to Create an Array in Python

What you’ll build or solve

You’ll create an “array” in Python using the right tool for your goal.

When this approach works best

Creating an array works best when you need:

  • A simple, flexible sequence for everyday code, like a list of names or tasks.
  • A memory-efficient typed array of numbers, like sensor readings or counters.
  • Fast numeric work on large datasets, like calculations on thousands of values.

Avoid reaching for a “real array” type when you just need to store a few mixed values. A plain list is often the cleanest option.

Prerequisites

  • Python installed
  • You know what a variable and a loop are

Step-by-step instructions

1) Create a list (the most common “array” in Python)

In Python, most people mean a list when they say “array”. Lists can hold mixed types and grow or shrink easily.

# empty list
items= []

# list with initial values
numbers= [3,1,4]
mixed= ["Naomi",2,True]

print(numbers)
print(items)

What to look for: Lists are dynamic, so you can append new values at any time, even if they are different types.

You can also add or update values easily:

numbers.append(10)
numbers[0]=99

print(numbers)

2) Create a typed array with the built-in array module

If you want “an array of numbers” where every element has the same type, use array.array. This is useful for compact storage and predictable numeric types.

fromarrayimportarray

# 'i' means signed int
counts=array("i", [1,2,3,4])

# 'f' means float
temps=array("f", [18.5,19.0,20.25])

print(counts)
print(temps)

What to look for: array("i", ...) only accepts integers. Passing a float raises an error instead of silently converting.

You can append values safely:

counts.append(5)
print(counts)

Type codes matter. Some common ones:

  • "i" → signed integer
  • "f" → float
  • "d" → double-precision float

Pick the one that matches your data.


3) Use NumPy when you need fast math on large arrays

For data science and heavy numeric work, NumPy arrays are the standard. They support fast vectorized operations like adding a value to every element.

importnumpyasnp

a=np.array([1,2,3,4])
b=a*10

print(a)
print(b)

What to look for: This step requires NumPy installed. If you see ModuleNotFoundError: No module named 'numpy', install it with:

pip install numpy

NumPy arrays allow powerful operations:

print(a.mean())
print(a.max())

These operations run efficiently, even on large datasets.


Examples you can copy

Example 1: Build an array from user input (list of ints)

text=input("Enter numbers separated by commas: ")
parts= [p.strip()forpintext.split(",")ifp.strip()]

nums= [int(p)forpinparts]
print(nums)

Example 2: Preallocate a fixed-size list

This is useful when you know the final size and want to fill values by index.

n=5
values= [0]*n

values[0]=10
values[1]=20
print(values)# [10, 20, 0, 0, 0]

Example 3: Create a 2D “array” (matrix) as a list of lists

rows=3
cols=4

grid= [[0for_inrange(cols)]for_inrange(rows)]
grid[1][2]=7

print(grid)

This ensures each row is a separate list.


Example 4: Create a typed array and append values safely

fromarrayimportarray

scores=array("i")

forxin [10,20,30]:
scores.append(x)

print(scores)

Example 5: Convert a list of numbers into a NumPy array and compute stats

importnumpyasnp

data= [2.5,3.0,4.5,1.0]
arr=np.array(data)

print(arr.mean())
print(arr.max())

Common mistakes and how to fix them

Mistake 1: Creating a 2D grid with repeated references

What you might do

grid= [[0]*3]*2
grid[0][0]=9
print(grid)# both rows changed

Why it breaks

[[0] * 3] * 2 repeats the same inner list object, so edits affect every row.

Fix

Create a new inner list for each row.

grid= [[0]*3for_inrange(2)]
grid[0][0]=9
print(grid)# only the first row changed

Mistake 2: Using array.array with the wrong type

What you might do

fromarrayimportarray

temps=array("i", [18.5,19.0])

Why it breaks

Type code "i" only accepts integers.

Fix

Pick the correct type code, like "f" for floats.

fromarrayimportarray

temps=array("f", [18.5,19.0])
print(temps)

Mistake 3: Expecting a NumPy array without installing NumPy

What you might do

importnumpyasnp
arr=np.array([1,2,3])

Why it breaks

NumPy is not part of the standard library.

Fix

Install NumPy, then retry.

pip install numpy

Troubleshooting

If you see ModuleNotFoundError: No module named 'numpy', run pip install numpy, or use a list if you do not need NumPy.

If you see TypeError when creating array("i", ...), check the type code and your values. Use "f" for floats.

If your 2D list updates multiple rows at once, you probably used [[...]] * n. Rebuild it with a loop or a comprehension.

If pip installs to a different Python than the one you run, try:

python-m pip install numpy

Then run your script with the same python.


Quick recap

  • Use a list for a flexible “array” in everyday Python.
  • Use array.array for compact, typed numeric arrays.
  • Use NumPy arrays for fast math on large datasets.
  • Build 2D arrays with a comprehension, not [[...]] * n.