Python uses lists, not arrays, for most everyday work
Python does not have a built-in array type the way some other languages do. Instead, you use a list, which is a container that holds multiple items in order. A list works like an array in almost every practical way: you can store numbers, text, or any other Python object inside it, access items by position, and change items after you create the list.
If you need a true array with stricter rules about data types, Python has a separate array module and a third-party library called NumPy. But for most programs, a list is what you want.
Key Takeaways
- Create a list by putting items inside square brackets: my_list = [1, 2, 3] creates a list of three numbers.
- Access items by their position using the index in square brackets, starting from 0: my_list[0] returns the first item.
- Add items to a list with the append() method or create an empty list with my_list = [] and fill it later.
- Use the array module or NumPy only if you need strict type checking or are working with large numerical datasets.
Creating a list with items already inside
The simplest way to create a list is to type the items you want, separated by commas, inside square brackets. Each item can be a number, a word in quotes, or anything else Python understands.
Here are three examples:
numbers = [1, 2, 3, 4, 5] names = ["Alice", "Bob", "Charlie"] mixed = [1, "hello", 3.14, True]The first list holds five numbers. The second holds three names as text. The third mixes different types together — Python allows this. You can also create a list that spans multiple lines to make it easier to read:
shopping_list = [ "milk", "eggs", "bread", "cheese" ]Creating an empty list and adding items later
Sometimes you do not know what items you want in the list when you create it. Start with empty square brackets and add items as you go using the append() method.
The append() method adds one item to the end of the list each time you call it. If you want to add multiple items at once, use the extend() method:
my_list = [1, 2, 3] my_list.extend([4, 5, 6]) print(my_list) # Output: [1, 2, 3, 4, 5, 6]Accessing items in a list by position
Once you have a list, you can get individual items by their position, called the index. Python counts positions starting from 0, not 1. The first item is at index 0, the second at index 1, and so on.
fruits = ["apple", "banana", "cherry"] print(fruits[0]) # Output: apple print(fruits[1]) # Output: banana print(fruits[2]) # Output: cherryYou can also count backward from the end using negative numbers. Index -1 is the last item, -2 is the second-to-last, and so on:
fruits = ["apple", "banana", "cherry"] print(fruits[-1]) # Output: cherry print(fruits[-2]) # Output: bananaChanging items and removing them
To change an item that already exists, use its index and the equals sign:
colors = ["red", "green", "blue"] colors[1] = "yellow" print(colors) # Output: ["red", "yellow", "blue"]To remove an item, use the remove() method if you know the item's value, or the pop() method if you know its position:
colors = ["red", "green", "blue"] colors.remove("green") # Remove by value print(colors) # Output: ["red", "blue"] numbers = [10, 20, 30, 40] numbers.pop(1) # Remove the item at index 1 (which is 20) print(numbers) # Output: [10, 30, 40]Using the array module for stricter type control
Python's built-in array module creates arrays that hold only one data type. This is useful if you want to prevent mistakes — for example, making sure a list contains only integers and nothing else. Import the module and specify the type code:
import array my_array = array.array('i', [1, 2, 3, 4, 5]) print(my_array) # Output: array('i', [1, 2, 3, 4, 5])The 'i' means the array holds integers. Other common type codes are 'f' for floating-point numbers and 'd' for double-precision floats. If you try to add a different type, Python will raise an error. For most everyday programs, a regular list is simpler and more flexible. Use the array module only when you specifically need type enforcement.
Using NumPy for large numerical datasets
If you are working with large amounts of numerical data or doing mathematical operations, the third-party library NumPy offers arrays that are much faster than lists. NumPy is not built into Python, so you must install it first using a package manager like pip.
import numpy as np my_array = np.array([1, 2, 3, 4, 5]) print(my_array) # Output: [1 2 3 4 5]NumPy arrays work similarly to lists but are optimized for speed and mathematical operations. They are the standard choice for data science, machine learning, and scientific computing. For a simple program that just needs to store a few items, a regular Python list is enough.
Frequently Asked Questions
What is the difference between a list and an array in Python?
A list is Python's built-in container that can hold any mix of data types. An array (from the array module) holds only one data type and is stricter about what you put in it. For most programs, a list is what you want.
How do I find the length of a list?
Use the len() function: len([1, 2, 3]) returns 3. This tells you how many items are in the list.
Can I create a list with a specific size filled with zeros?
Yes, use multiplication: my_list = [0] * 5 creates a list with five zeros. You can then change individual items as needed.
What happens if I try to access an index that does not exist?
Python raises an IndexError. For example, if your list has three items and you try to access index 5, the program will stop and show an error message.
Should I use a list or NumPy array?
Use a list for everyday programs and small datasets. Use NumPy if you are doing heavy mathematical work, working with large numerical datasets, or need specialized functions for data science.