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 size | Nested arrays has to be of same size. |
Can perform direct operations using functions like: No need to import anything to use these functions. |
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]) |
Difference between List and Array in Python
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.
Contact Us