My web dev diary 📔✨

Notes on my journey learning to code with Python & Django

Study notes: Arrays vs lists in Python

AMartin1987 | Published on: Dec. 27, 2023, 8:48 p.m.

 

An array is a linear data structure that stores a collection of similar data types at continuous locations in a computer's memory.

An representation of an array in memory. Source: GeekForGeeks

Like arrays, lists are also data structures used to store multiple items.

Quoting the excellent LearnPython article, both arrays and lists in Python share some traits: 

  • Their items are enclosed in square brackets, like this [item1, item2, item3].
  • They are ordered – i.e. their items appear in a specific order. This enables us to use an index to access to any item.
  • They are mutable, which means you can add or remove items after their creation.
  • Their elements do not need to be unique. Item duplication is possible, as each element has its own distinct place and can be accessed separately through the index.

Example of a list in Python: 

list = [3, 6, 9, 12]
print(list)
print(type(list))

Result:

[3, 6, 9, 12]
<class 'list'>

However, lists are different from arrays in that their elements can always be of different data types: you can combine strings, integers, and objects in the same list. In contrast, when it comes to the array's ability to store different data types, it depends on the kind of array used.

To use arrays in Python, you need to import either an array module or a NumPy package.

import array as arr
import numpy as np

The Python array module requires all array elements to be of the same type. Moreover, to create an array, you'll need to specify a value type. In the code below, the "i" signifies that all elements in array_1 are integers:

array_1 = arr.array("i", [3, 6, 9, 12])
print(array_1)
print(type(array_1))

Result:

array('i', [3, 6, 9, 12])
<class 'array.array'>

On the other hand, NumPy arrays support different data types. To create a NumPy array, you only need to specify the items (enclosed in square brackets):

array_2 = np.array(["numbers", 3, 6, 9, 12])
print (array_2)
print(type(array_2))

Result:

['numbers' '3' '6' '9' '12']
<class 'numpy.ndarray'>

As you can see, array_2 contains one item of the string type (i.e., "numbers") and four integers.

Moreover, arrays are different from lists in that:

  • Arrays need to be declared. Lists don't, since they are built into Python. In the examples above, you saw that lists are created by simply enclosing a sequence of elements into square brackets. Creating an array, on the other hand, requires a specific function from either the array module (i.e., array.array()) or NumPy package (i.e., numpy.array()). Because of this, lists are used more often than arrays.
  • Arrays can store data very compactly and are more efficient for storing large amounts of data.
  • Arrays are great for numerical operations; lists cannot directly handle math operations. For example, you can divide each element of an array by the same number with just one line of code. If you try the same with a list, you'll get an error.
  • You need a loop to access each element in an array, but this is not case with lists. 
  • Being a compact block in memory, arrays require less memory than lists.

Categories:

 

Computer science  

Python  

About me
notByAI badge