Skip to content

Python list vs array

  • by

Python List vs Array provides two main data structures for storing collections of elements. While both can be used to store multiple values, they have some key differences in terms of functionality and the underlying implementation.

Here’s a tabular format comparing Python lists and arrays:

FeaturePython ListsArrays (NumPy)
FlexibilityCan store elements of different data types Dynamically resizableNumPy library provides powerful ndarray the object for advanced numerical computations
Restricted to elements of the same type Fixed-size upon creationMore memory and processing overheadMore efficient for numerical computations
FunctionalityRich set of built-in methods and operationsA limited set of methods focused on math
External LibrariesNo external library requiredNumPy library provides powerful ndarray object for advanced numerical computations

Here’s a comparison of the syntax for Python lists and arrays:

List

Creating a List:

my_list = [1, 2, 3, 4, 5]

Accessing Elements in a List:

print(my_list[0])  # Output: 1
print(my_list[2])  # Output: 3

Modifying Elements in a List:

my_list[3] = 10
print(my_list)  # Output: [1, 2, 3, 10, 5]

List Methods:

my_list.append(6)
print(my_list)  # Output: [1, 2, 3, 10, 5, 6]

my_list.remove(3)
print(my_list)  # Output: [1, 2, 10, 5, 6]

Array

Creating an Array (NumPy):

import numpy as np

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

Accessing Elements in an Array:

print(my_array[0])  # Output: 1
print(my_array[2])  # Output: 3

Modifying Elements in an Array:

my_array[3] = 10
print(my_array)  # Output: [ 1  2  3 10  5]

Array Functions (NumPy):

my_array = np.append(my_array, 6)
print(my_array)  # Output: [ 1  2  3 10  5  6]

my_array = np.delete(my_array, 2)
print(my_array)  # Output: [ 1  2 10  5  6]

Python list vs array example

Here’s an example that demonstrates the differences between a Python list and an array:

# Importing the required modules
import array
import numpy as np

# Python List
my_list = [1, 2, 3, 4, 5]  # A list containing integers
my_list.append(6)          # Adding an element at the end
my_list.remove(3)          # Removing an element

print("List:", my_list)    # Output: List: [1, 2, 4, 5, 6]

# Array
my_array = array.array('i', [1, 2, 3, 4, 5])  # An array of integers
my_array.append(6)                           # Adding an element at the end (supported in arrays)
my_array.remove(3)                           # Error: 'array.array' object has no attribute 'remove'

print("Array:", my_array) 

Output:

Python list vs array

This example showcases the flexibility and functionality of lists compared to the more specialized nature of arrays.

Do comment if you have any doubts or suggestions on this Python difference topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading