How to Distinguish Between Arrays and Lists in Python?

In this tutorial, I will explain how to distinguish between arrays and lists in Python. Someone asked me this doubt during a Python webinar and this was the topic of discussion. So I decided to write an article on this. We will explore various methods to distinguish between arrays and lists with examples and screenshots of executed example code.

What is a List in Python?

A list in Python is a built-in data structure that can store a collection of items. Lists can contain elements of different data types, such as integers, strings, and even other lists.

Check out setting an array element with a sequence error in Python

Characteristics of Lists

  • Heterogeneous Elements: Lists can store elements of different types.
  • Dynamic Size: Lists can grow and shrink as needed.
  • Mutable: You can modify elements in a list after its creation.
  • Built-in Methods: Lists come with a variety of built-in methods such as append(), remove(), sort(), and more.

Example of a List

# Creating a list of American cities
cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]

# Adding a new city
cities.append("Philadelphia")

# Removing a city
cities.remove("Houston")

# Sorting the list
cities.sort()

print(cities)

Output:

 ['Chicago', 'Los Angeles', 'New York', 'Philadelphia', 'Phoenix']

I executed the above example code and added its screenshot below.

Distinguish Between Arrays and Lists in Python

In the example above, we created a list of cities in the USA, added a new city, removed one, and sorted the list.

Read Python program to print the smallest element in an array

What is an Array in Python?

An array in Python is a data structure that can store a collection of items, but unlike Python lists, arrays are different in terms of data type. Arrays are typically used for numerical operations and require the specification of a data type.

Characteristics of Arrays

  • Homogeneous Elements: Python Arrays can only store elements of the same type.
  • Fixed Size: Arrays have a fixed size, which means you need to define the number of elements it can hold.
  • Efficient for Numerical Operations: Python Arrays are optimized for numerical computations.
  • Requires Import: Arrays are not built-in and require importing from a module like array or numpy.

Example of an Array

import numpy as np

# Creating an array of temperatures in Fahrenheit
temperatures = np.array([32, 45, 50, 60, 72, 85])

# Adding a new temperature (requires creating a new array)
temperatures = np.append(temperatures, 90)

# Removing a temperature (requires creating a new array)
temperatures = np.delete(temperatures, 2)

# Dividing all temperatures by 2
temperatures = temperatures / 2

print(temperatures)

Output:

[16.  22.5 30.  36.  42.5 45. ]

I executed the above example code and added its screenshot below.

How to Distinguish Between Arrays and Lists in Python

In the example above, we created an array of temperatures, added a new temperature, removed one, and performed a numerical operation on all elements.

Check out How to Split a String into an Array in Python

When to Use Lists

Use Case

For example, think you are developing a web application for a travel agency. You need to store information about various destinations, including city names, population, and tourist attractions.

# List of destinations
destinations = [
    {"city": "New York", "population": 8419000, "attractions": ["Statue of Liberty", "Central Park"]},
    {"city": "Los Angeles", "population": 3980000, "attractions": ["Hollywood Sign", "Venice Beach"]},
    {"city": "Chicago", "population": 2716000, "attractions": ["Millennium Park", "Navy Pier"]}
]

# Adding a new destination
destinations.append({"city": "Houston", "population": 2328000, "attractions": ["Space Center", "Houston Zoo"]})

print(destinations)

In this example, a list of dictionaries is used to store information about different destinations, demonstrating the flexibility of lists.

Read 3D Arrays in Python

When to Use Arrays

Use Case

For example, Imagine you are working on a data analysis project to study the temperature of various US cities. You need to perform statistical operations on large datasets of temperature readings.

import numpy as np

# Array of temperature readings
temperature_readings = np.array([72, 75, 78, 80, 82, 85, 88, 90, 92, 95])

# Calculating the mean temperature
mean_temperature = np.mean(temperature_readings)

# Finding the maximum temperature
max_temperature = np.max(temperature_readings)

# Normalizing the temperature readings
normalized_temperatures = (temperature_readings - mean_temperature) / np.std(temperature_readings)

print("Mean Temperature:", mean_temperature)
print("Max Temperature:", max_temperature)
print("Normalized Temperatures:", normalized_temperatures)

In this example, an array is used to store temperature readings and perform statistical operations efficiently.

Check out How to Print Duplicate Elements in Array in Python

Summary Table

FeatureListsArrays
Data TypeHeterogeneousHomogeneous
SizeDynamicFixed
PerformanceSlower for numerical operationsFaster for numerical operations
Built-in MethodsExtensive built-in methodsRequires external libraries for advanced ops
Use CaseGeneral-purpose, flexible data storageNumerical computations, large datasets

Check out How to Convert an Array to a Tuple in Python

Conclusion

In this tutorial I have shown how to distinguish between arrays and lists in Python, I explained lists and arrays with their characteristics and examples, I also discussed the use cases of both lists and arrays, and I added a summary table for your reference which helps you to understand better.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.