Array vs List in Python - Demystifying the Differences

Posted in /  

Array vs List in Python - Demystifying the Differences
pankajbhadwal

Pankaj Bhadwal
Last updated on April 19, 2024

    ​Array and list are two popular and widely used data structures to store multiple values. The main difference between them (Array vs List) is that an array is a collection of homogeneous data elements, while a list is a heterogeneous collection of data elements. This means that the list can be homogeneous or heterogeneous, thus containing elements of different data types.

    This was the primary difference between the array and the list in Python.

    Homogeneous data elements refer to the elements of the same data type. Heterogeneous data elements refer to values that are of different data types.

    Another major difference between these two data structures is that a list is readily available in Python, i.e., you can use it directly without importing anything. Conversely, Python does not offer built-in support for the array. You first need to import the array module or numpy and then use it in a program.

    Let's continue this Array vs List discussion to learn more differences between the two data structures .

    Array vs List: A Comprehensive Comparison

    Array

    List

    An Array can store only homogenous items, i.e., the elements of the same data type.

    The list can store heterogeneous items, i.e., elements of different data types.

    The size of the array is defined during initialization, making it a fixed-sized data structure.

    It is a dynamic data structure, as it does not have a fixed size.

    The elements of the array are known as items.

    The elements of the list are known as nodes.

    An array supports indexing, and the index number of elements starts from 0 to n-1.

    A list does not support indexing.

    Due to indexing, the time complexity of accessing an individual item is O(1).

    The time complexity of accessing an individual Node of a list is O(N).

    An array can handle arithmetic operations.

    A list cannot handle arithmetic operations directly.

    It is ideal for storing shorter sequences of data.

    It is ideal for storing longer sequences of data.

    Big arrays often lead to memory wastage.

    The list is not susceptible to memory wastage.

    The insertion or deletion of an item in an array may affect every array item.

    This is not the case in the list. One or two nodes are affected while inserting or deleting a list node.

    The elements of an array are displayed by using an explicit loop.

    A list does not require an explicit loop to print its elements.

    What is an Array ?

    An array is a linear data structure that contains homogenous elements or elements of the same data type. Elements in an array are stored in consecutive order, i.e., at contiguous memory locations.

    Almost all high-level programming languages - except Python (not directly) - support arrays. The following are the different operations performed on an array -

    • Accessing
    • Inserting
    • Deleting
    • Traversing

    Python does not have the concept of arrays, but you can use arrays in Python by importing the array module or importing its default numerical computation library, NumPy .

    1. Import Array Module

    Syntax

    import array as arr

    Example

    import array as arr
    
    arr_elements = arr.array("i", [31, 60, 19, 12])
    print(arr_elements)
    print(type(arr_elements))

    Output

    array('i', [31, 60, 19, 12])
    <class 'array.array'>
    1. Import Numpy

    Syntax

    import numpy as np

    Example

    import numpy as np
    
    arr_elements = np.array([1, 2, 3, 4])
    print(arr_elements)
    print(type(arr_elements))

    Output

    [1234]
    <class 'numpy.ndarray'>

    Features

    • An array is a homogeneous collection of elements.
    • It always stores elements in a contiguous memory location. Hence, performing operations on array elements is much easier.
    • Total bytes = size of(Datatype) * size of an array.
    • It stores elements in an ordered sequence. Hence, it is called a linear data structure. Each element has an integer index, and the index starts from 0. For instance, in my_array[34, 56, 67], the elements ‘34’, ‘56’, and ‘67’, are stored at indices ‘0’, ‘1’, and ‘2’, respectively.
    • It has the property of finiteness. This means that every array contains a finite number of elements.
    • The element of an array can be accessed by its corresponding index number.

    Advantages

    • You can store multiple data elements by the same name.
    • An array is very fast to use.
    • With the help of the index, you can access any element with the time complexity of O(1).
    • Other data structures can also be implemented using arrays, such as lists, stacks, and queues.
    • Numerical calculations, like matrix operations, are easy to perform on this data structure.
    • You can find the address of any element of an array. For that, you need to know the address of the first element.
    • You can represent matrices using 2D arrays.

    Disadvantages

    • An array is finite. This finiteness can lead to the problem of memory wastage if you declare the size of the array as more than the elements that it needs to store.
    • Since the array keeps its elements in a contiguous memory location, you cannot initialize the array if your memory space is sufficient for the array but not contiguous.
    • Its size cannot increase or decrease during the runtime.
    • The time complexity of insertion and deletion of elements is O(n).

    What is a List?

    A list is similar to an array in terms of purpose, i.e., to store elements. The only difference is that a Python list can store heterogeneous data elements, too, i.e., elements of different data types. All items in a list are ordered, changeable, and duplicated.

    The operations performed on the list are more efficient than the arrays. The following are the operations performed on a list:

    • Operations to Perform on Lists
    • Inserting
    • Deletion
    • Traversing
    • Sorting
    • Searching for a given element
    • Checking the size of the list
    • Checking whether the list is empty or not
    • Copying a list to another list

    Syntax

    var = ['item 1', 'item 2', ...]

    Example

    sample_list = [3, 'Python', 56.76, 'Tutorial']
    print(sample_list)

    Output

    [3, 'Python', 56.76, 'Tutorial']

    Features

    • As mentioned above, lists are flexible in terms of data storage. It can store elements of any data type, as you can see in the above example.
    • Python lists are ordered. You can access the elements in a list using their indices. The index of the first element is 0.
    sample_list = [3, 'Python', 56.76, 'Tutorial']
    print(sample_list[1])

    Output

    Python
    • It also supports negative indices to access the elements from the end of the list.
    print(sample_list[-2])

    Output

    56.76
    • You can easily mutate a list after initializing. For instance, if you want to change the value of an element with the index number 2, you can assign the new value to it as follows:
    sample_list[2] = 'ArrayList'
    print(sample_list)

    Output

    [3, 'Python', 'ArrayList', 'Tutorial']
    • The concept of nested lists enables you to create multi-dimensional lists. They can also serve as multi-dimensional arrays in Python.
    my_list = [[1, 'Insert', 'Delete'], [45.87, 56, 'Sorting', 'Traversing']]
    print(my_list)

    Output

    [[1, 'Insert', 'Delete'], [45.87, 56, 'Sorting', 'Traversing']]

    Advantages

    • A list is a dynamic data structure allowing you to add new elements and delete existing ones during runtime.
    • It is immune to memory wastage. In a list, you can define the size of the list according to the elements required.
    • It’s easier to perform the insertion and deletion operations on the list.
    • Other data structures, such as stacks and queues, can be implemented through lists.

    Disadvantages

    • Compared to arrays, lists require more memory space because of the additional pointer requirement.
    • Traversing the list is difficult.

    Conclusion

    That wraps up this array vs list comparison. Both are popular data structures to store data. While a list stores elements of different data types, an array stores elements of the same data type. When you gain a clear understanding of both these data structures and their differences, you can easily pick one depending on your project requirements.

    We hope this article was helpful in understanding the difference between the array and the list. Feel free to comment below in case of any doubts or queries.

    People are also reading:

    FAQs


    An array stores the data items of the same type, whereas a list stores the data items of the different types.

    A list is more suitable for storing data in Python because of many reasons, such as the ability to store any type of data and resizable to store any number of data items.

    When you have a large amount of the same type of data, an array is ideal to use. On the other hand, a list is the best choice when you have a small amount of data with different types.

    When you have a large amount of data of the same type, you must use an array as it consumes less memory. Go with a list when you have data of different types.

    As an array stores the data elements of the same type, it is faster than a list (which stores heterogeneous data items).

    Leave a Comment on this Post

    0 Comments