An array in Python is a container that holds multiple values of the same type in a single variable

Python does not have a built-in array type the way some other languages do. Instead, Python uses lists, which work like arrays but are more flexible — they can hold different types of data in the same list. If you need a true array with stricter type rules, you can use the array module or NumPy, a library built for numerical computing. For most web development and general programming, a list is what you want.

The simplest way to create a list is to put values inside square brackets, separated by commas. Python keeps track of the position of each item, starting from zero. You can add items, remove them, change them, or loop through them — all the things you would do with an array in other languages.

Key Takeaways

  • A Python list is created by putting values inside square brackets: my_list = [1, 2, 3]
  • List positions start at zero, so the first item is at index 0, the second at index 1, and so on.
  • You can add items with .append(), remove them with .remove(), and access them by their position number.
  • The array module creates stricter arrays if you need all items to be the same type, but lists are more common in Python.
  • NumPy arrays are used for math and data work, but require installing the NumPy library first.

Creating a list with square brackets

The most direct way to create a list is to type the values you want inside square brackets. Here is a list of three numbers:

my_numbers = [10, 20, 30]

Here is a list of words:

my_words = ["apple", "banana", "cherry"]

You can also create an empty list and add items later:

my_list = []

A list can hold different types at once — numbers, words, and other values in the same list. This is one way Python lists differ from arrays in languages like Java or C.

Accessing items by their position

Each item in a list has a position number called an index. The first item is always at index 0, the second at index 1, and so on. To get an item, put its index in square brackets after the list name:

my_words = ["apple", "banana", "cherry"] print(my_words[0]) — prints "apple" print(my_words[1]) — prints "banana" print(my_words[2]) — prints "cherry"

You can also count backward from the end. Index -1 is the last item, -2 is the second-to-last, and so on:

print(my_words[-1]) — prints "cherry" print(my_words[-2]) — prints "banana"

If you try to access an index that does not exist, Python will stop and show an error. A list with three items has indexes 0, 1, and 2 — nothing at index 3.

Adding and removing items

The .append() method adds a single item to the end of a list:

my_list = [1, 2, 3] my_list.append(4) print(my_list) — prints [1, 2, 3, 4]

The .remove() method removes the first item that matches the value you give it:

my_list = [1, 2, 3, 2] my_list.remove(2) print(my_list) — prints [1, 3, 2]

The .pop() method removes an item by its index and gives you back the value that was removed:

my_list = ["a", "b", "c"] removed = my_list.pop(1) print(removed) — prints "b" print(my_list) — prints ["a", "c"]

If you do not give .pop() an index, it removes and returns the last item.

Looping through a list

A for loop lets you do something with each item in a list, one at a time. This is how you process all the data in an array:

my_numbers = [10, 20, 30] for number in my_numbers:   print(number)

This prints 10, then 20, then 30 — each on its own line. The variable number holds each item as the loop runs.

If you need the position as well as the value, use the range() and len() functions:

my_words = ["apple", "banana", "cherry"] for i in range(len(my_words)):   print(i, my_words[i])

This prints the index and the word: 0 apple, 1 banana, 2 cherry. The len() function tells you how many items are in the list.

Using the array module for stricter arrays

If you need all items in your array to be the same type — all integers, or all floating-point numbers — you can use Python's built-in array module. This is closer to how arrays work in other languages:

import array my_array = array.array('i', [1, 2, 3, 4])

The 'i' means the array holds signed integers. Other type codes include 'f' for floating-point numbers and 'd' for double-precision floats. If you try to add a different type, Python will refuse.

Arrays from the array module use less memory than lists and are faster for large amounts of numeric data. However, they are less flexible. For most web development and general programming, a list is simpler and more practical.

NumPy arrays for math and data work

If you are doing math, statistics, or data analysis, the NumPy library provides arrays built for that work. NumPy arrays are faster and more powerful than Python lists for numerical operations:

import numpy as np my_array = np.array([1, 2, 3, 4])

NumPy is not part of Python's standard library, so you have to install it first using a package manager like pip. Once installed, you can do math on entire arrays at once without writing a loop:

my_array = np.array([1, 2, 3, 4]) result = my_array * 2 print(result) — prints [2 4 6 8]

NumPy is the standard choice for scientific computing in Python. If you are building a website or writing general-purpose code, a regular list is what you need. If you are working with large datasets or doing calculations, NumPy is worth learning.

Frequently Asked Questions

What is the difference between a list and an array in Python?

Python lists are flexible containers that can hold different types of data. Arrays from the array module or NumPy require all items to be the same type and use less memory. For most programming, lists are what you use.

Why does Python start counting at zero instead of one?

Most programming languages use zero-based indexing because it makes the math simpler under the hood. The index represents how far from the start of the list an item is — the first item is zero positions away, the second is one position away, and so on.

Can I change an item that is already in a list?

Yes. Use the index to access it and assign a new value: my_list[1] = "new value". This replaces whatever was at position 1 with the new value.

What happens if I try to access an index that does not exist?

Python stops and shows an IndexError. You can avoid this by checking the length of the list first with len(), or by using a for loop instead of accessing by index.

Should I use NumPy or the array module for my project?

Use a regular list unless you have a specific reason not to. Use the array module if you need strict type checking and lower memory use. Use NumPy if you are doing math, statistics, or working with large datasets.