Python: Differences Between List and Array

List:

In Python, a list is a collection of items that can contain elements of multiple data types, such as numeric, character logical values, and so on. It is an ordered collection that allows for negative indexing. Using [], you can create a list with data values.
List contents may be simply merged and copied using Python’s built-in functions.

The core difference between a Python list and a Python array is that a list is included in the Python standard package, whereas an array requires the “array” module to be imported. With a few exceptions, lists in Python replace the array data structure.

Array:

An array is a vector that contains homogenous items, that is, elements of the same data type. Elements are assigned contiguous memory addresses, allowing for easy change, i.e., addition, deletion, and access to elements. To declare arrays in Python, we must utilize the array module. If the array’s items are of different data types, an exception “Incompatible data types” is issued.

Differences: List vs Array

                                    LIST                                    ARRAY
Elements of many data types may be present in a list. Only elements of the same data type are included in an array
There is no need to import a module manually for declaration. It is necessary to explicitly import a module for declaration.
Cannot do mathematical operations directly. Can do arithmetic operations directly in an array
Preferred for shorter data item sequences Preferred for longer data sequences.
It is possible to nest elements to contain multiple types of elements. All nested items of the same size must be present.
Data can be easily modified (added, deleted) with greater freedom. Less flexibility because addition and deletion must be done element by element.
Without any explicit looping, the complete list can be printed. To print or access the array’s components, a loop must be created.
Lists Consume more memory to facilitate the insertion of elements. The consumed Memory size is somewhat smaller.

1)Storing Data – List vs Array:

Data structures, as we all know, are used to effectively store data.
In this scenario, a list can contain heterogeneous data values. In other words, data objects of various data types can be handled in a Python List.

list:

# Give the list as static input and store it in a variable.
# List may contain heterogenous datatypes like int, float, strings etc.
gvn_lst = [8, 2.5, 1, "hello", 'Python-programs']
# Print the given list
print(gvn_lst)

Output:

[8, 2.5, 1, 'hello', 'Python-programs']

Arrays:

Arrays, on the other hand, store homogenous elements into them, that is, elements of the same kind.

# Import array using the import keyword
import array
# Give the array as static input by passing it as an argument to
# the array() function and store it in a variable.
# Arrays contain homogeneous datatypes
gvn_arry = array.array('i', [15, 65, 25, 48])
# Print the given array
print(gvn_arry)

Output:

array('i', [15, 65, 25, 48])

2)Declaration

Lists: 

Python provides a built-in data structure called “List.” As a result, lists in Python do not need to be specified.

# Give the list as static input and store it in a variable.
gvn_lst = [8, 2.5, 1, "hello", 'Python-programs']

Arrays:

Arrays must be declared in Python. We can declare an array using the following methods:

Array Module:

import array
ArrayName = array.array('format-code', [items])

Numpy Module:

import numpy
ArrayName = numpy.array([items])

3)Mathematical Operations:

Arrays:

When it comes to conducting Mathematical operations, arrays have an advantage. The NumPy module provides us with an array structure to store and manipulate data values.

Example

# Import numpy module using the import keyword
import numpy
# Give the array as static input by passing it as an argument to
# the array() function and store it in a variable.
gvn_arry = numpy.array([15, 10, 5, 4])
# Multiply each element of the array with 5 and and store it in another variable.
rslt = gvn_arry*5
# Print the above result
print(rslt)

Output:

[75 50 25 20]

In contrast to lists, where the operations performed on the list do not reflect in the results, as shown in the example below using list operations.

In this case, we attempted to multiply the constant value (5) by the list, but the result does not have any effect. Because Lists cannot be directly mathematically manipulated with any data values.

So, if we wish to multiply 5 with the elements of the list, we must multiply 5 with each element of the list individually.

Lists:

# Give the list as static input and store it in a variable.
gvn_lst = [15, 10, 5, 4]
# Multiply the list with 5 and and store it in another variable.
rslt = gvn_lst*5
# Print the given lst
print(gvn_lst)

Output:

[15, 10, 5, 4]

4)Changing the size of the data structure

Python Lists, as an inbuilt data structure, may be enlarged or resized quickly and easily.

Arrays, on the other hand, show very poor performance when it comes to resizing the array’s memory. Instead, we’ll have to duplicate the array in order to scale and resize it.