Open In App
Related Articles

Difference between List and Array in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In Python, lists and arrays are the data structures that are used to store multiple items. They both support the indexing of elements to access them, slicing, and iterating over the elements. In this article, we will see the difference between the two.

Operations Difference in Lists and Arrays

Accessing element is fast in Python Arrays because they are in a contiguous manner but insertion and deletion is quite expensive because all the elements are shifted from the position of inserting and deleting element linearly. Suppose the array is of 1000 length and we are inserting/deleting elements at 100 position then all the elements after the hundred position will get shifted due to which the operation becomes expensive.

Accessing an element in a Python List is the same as an Array because a List is actually a dynamic array . Inserting or deleting elements at the beginning or in the middle of a list can be less efficient because it may require shifting all subsequent elements, which is a linear-time operation in the worst case.

What are Lists?

A list in Python is an inbuilt collection of items that can contain elements of multiple data types, which may be either numeric, character logical values, etc. It is an ordered collection supporting negative indexing. A list can be created using [] containing data values. Contents of lists can be easily merged and copied using Python’s inbuilt functions. 

Example:

In this example, we are creating a list in Python. The first element of the list is an integer, the second a Python string, and the third is a list of characters. 

Python3

# creating a list containing elements
# belonging to different data types
sample_list = [1, "Yash", ['a', 'e']]
print(type(sample_list))
print(sample_list)

                    

Output: 

<class 'list'>
[1, 'Yash', ['a', 'e']]

What are Arrays?

An array is a vector containing homogeneous elements i.e. belonging to the same data type. Elements are allocated with contiguous memory locations. Typically the size of an array is fixed. Th e insertion and deletion costs are high as compared to the list however indexing is faster in the Arrays due to contiguous memory allocation. Arrays can be used by importing the array module.

Example:

In this example, we will create a Python array by using the array() function of the array module and see its type using the type() function.

Python3

# importing "array" for array creations
import array as arr
  
# creating an array with integer type
a = arr.array('i', [1, 2, 3])
print(type(a))
for i in a:
    print(i, end=" ")

                    

Output:

<class 'array.array'>
1 2 3 

Difference Between List and Array in Python

The following table shows the differences between List and Array in Python:

List

Array

Can consist of elements belonging to different data types

Only consists of elements belonging to the same data type

No need to explicitly import a module for the declaration

Need to explicitly import the array module for declaration

Cannot directly handle arithmetic operations

Can directly handle arithmetic operations

Preferred for a shorter sequence of data items

Preferred for a longer sequence of data items

Greater flexibility allows easy modification (addition, deletion) of data

Less flexibility since addition, and deletion has to be done element-wise

The entire list can be printed without any explicit looping

A loop has to be formed to print or access the components of the array

Consume larger memory for easy addition of elements

Comparatively more compact in memory size

Nested lists can be of variable sizeNested arrays has to be of same size.

Can perform direct operations using functions like:
count() – for counting a particular element in the list
sort() – sort the complete list
max() – gives maximum of the list
min() – gives minimum of the list
sum() – gives sum of all the elements in list for integer list
index() – gives first index of the element specified
append() – adds the element to the end of the list
remove() – removes the element specified

No need to import anything to use these functions.
and many more…

Need to import proper modules to perform these operations.

Example:
my_list = [1, 2, 3, 4]
Example:
import array
arr = array.array(‘i’, [1, 2, 3])


Last Updated : 20 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads