Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to create an array in Python

Introduction

Arrays are an essential part of programming, and in this blog, we'll explore how to create arrays in Python. Arrays are data structures that allow us to store and manipulate multiple items of the same type in a single variable. They can be thought of as a container or a collection that holds a fixed number of items, like a row of lockers, where each locker can store one item of the same type.

Before we dive into creating arrays in Python, it's essential to clarify that Python does not have built-in support for arrays like some other programming languages (e.g., C, C++, and Java). However, Python provides similar data structures, such as lists and tuples, which can be used to create arrays.

In this blog, we'll discuss:

  1. Creating arrays using Python lists
  2. Creating arrays using Python tuples
  3. Creating arrays using the NumPy library

We'll also provide intuitions, analogies, and actual code examples to help you understand and implement arrays in Python.

Creating Arrays Using Python Lists

Python lists are mutable (i.e., they can be changed after they're created), ordered collections of items. Lists can hold items of different types, but when we want to create an array, we'll use lists to store only items of the same type. We can think of Python lists as a row of adjustable lockers, where the size of each locker can be changed to accommodate different types of items.

To create a list in Python, we use square brackets [] and separate the items with commas. Let's create a simple list of integers:

integer_array = [1, 2, 3, 4, 5]
print(integer_array)

Output:

[1, 2, 3, 4, 5]

We can also create an empty list and add items to it later using the append() method:

empty_array = []
empty_array.append(1)
empty_array.append(2)
empty_array.append(3)
print(empty_array)

Output:

[1, 2, 3]

Accessing and Modifying Items in a List

To access an item in a list, we use its index (position) in the list. Indices in Python start at 0, so the first item has an index of 0, the second item has an index of 1, and so on.

For example, let's access the first item in our integer_array:

first_item = integer_array[0]
print(first_item)

Output:

1

We can also modify items in a list by assigning a new value to the item at a specific index:

integer_array[0] = 10
print(integer_array)

Output:

[10, 2, 3, 4, 5]

Looping Through a List

We can loop through a list using a for loop. For example, let's print each item in our integer_array:

for item in integer_array:
    print(item)

Output:

10
2
3
4
5

Creating Arrays Using Python Tuples

Python tuples are similar to lists, but they are immutable (i.e., they cannot be changed after they're created). Tuples can also hold items of different types, but for creating arrays, we'll use tuples to store only items of the same type. We can think of Python tuples as a row of fixed-size lockers, where the size of each locker is determined when the tuple is created, and it cannot be changed afterward.

To create a tuple in Python, we use parentheses () and separate the items with commas. Let's create a simple tuple of integers:

integer_array = (1, 2, 3, 4, 5)
print(integer_array)

Output:

(1, 2, 3, 4, 5)

Accessing Items in a Tuple

Accessing items in a tuple is the same as accessing items in a list. We use the item's index (position) in the tuple:

first_item = integer_array[0]
print(first_item)

Output:

1

However, since tuples are immutable, we cannot modify their items by assigning a new value to an index. Attempting to do so will result in an error:

integer_array[0] = 10

Output:

TypeError: 'tuple' object does not support item assignment

Looping Through a Tuple

Looping through a tuple is the same as looping through a list. We can use a for loop to print each item in our integer_array:

for item in integer_array:
    print(item)

Output:

1
2
3
4
5

Creating Arrays Using the NumPy Library

While Python lists and tuples are versatile and easy to use, they may not be the most efficient way to handle arrays, especially when dealing with large datasets or performing complex mathematical operations. NumPy (short for Numerical Python) is a popular library for scientific computing in Python that provides a powerful, multidimensional array object called numpy.ndarray.

NumPy arrays are similar to Python lists and tuples, but they are specifically designed for numerical operations and provide better performance and additional features. We can think of NumPy arrays as specialized lockers that are optimized for storing and manipulating numbers.

To use NumPy, we first need to install it using pip:

pip install numpy

After installing NumPy, we can create an array by importing the library and using the numpy.array() function:

import numpy as np

integer_array = np.array([1, 2, 3, 4, 5])
print(integer_array)

Output:

[1 2 3 4 5]

Accessing and Modifying Items in a NumPy Array

Accessing and modifying items in a NumPy array is similar to Python lists and tuples. We use the item's index (position) in the array:

first_item = integer_array[0]
print(first_item)

Output:

1

To modify an item, we can assign a new value to the item at a specific index:

integer_array[0] = 10
print(integer_array)

Output:

[10  2  3  4  5]

Looping Through a NumPy Array

Looping through a NumPy array is the same as looping through a list or tuple. We can use a for loop to print each item in our integer_array:

for item in integer_array:
    print(item)

Output:

10
2
3
4
5

Conclusion

In this blog, we've explored how to create arrays in Python using lists, tuples, and the NumPy library. We've discussed how to access, modify, and loop through these various types of arrays, and provided intuitions and analogies to help you understand the concepts.

Now that you're familiar with creating arrays in Python, you can use them to store and manipulate data in your programming projects. Remember to choose the appropriate data structure for your specific use case, whether it's a versatile Python list, an immutable tuple, or a powerful NumPy array for numerical operations. Happy coding!