In Python, both list and array represent a list of data, that is, different pieces of data arranged in sequence. List and array are very similar data structures and often confuse developers. These data structures are used to store serial data. However, they differ in aspects of performance and memory utilisation. In this article, we will start by understanding lists and arrays in Python. We will start with the basics of the two types, and then later, we will look at the differences between them.
List in Python
The list is one of the pre-existing data structures implemented in Python that can store different kinds of data. Currently, lists are very versatile in their structures and are complemented by a huge number of integrated functions for working with data.
Here, we have covered some operations which we can perform on the list.
Creating List
They include the use of square brackets and a sequence of elements to produce a list.
Example:
In the below code, we created two lists. We have one list for integers, which contains only integer data types, and another list has a variety of data types, i.e. it contains heterogeneous data.
l1=[1, 2, 3, 4, 5]
l2=[1, ‘a’, 3 ,’b’, 4.5]
print(l1)
print(l2)
Output:
[1, 2, 3, 4, 5]
[1, ‘a’, 3 ,’b’, 4.5]
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure
Array in Python
Array is one of the most basic structures for storing data, and it is applied only to homogeneous data. In Python, an array is not among the native data types. Thus, one must depend on the library to employ an array.
Now, let’s discuss the various activities that can be done with arrays.
Creating Array
We can make use of additional libraries like Array or NumPy to create arrays in Python.
Using array library:
import array
arr1=array.array(‘i’, [1, 2, 3, 4])
arr2=array.array(‘d’, [2.4, 3.1, 8.9])
print(arr1)
print(arr2)
Output:
array(‘i’, [1, 2, 3, 4])
array(‘d’, [2.4, 3.1, 8.9])
Using numpy library:
import numpy as np
arr1=np.array([1, 2, 3, 4])
arr2=np.array([2.4, 3.1, 8.9])
print(arr1)
print(arr2)
Output:
[1, 2, 3, 4]
[2.4, 3.1, 8.9]
Difference between List and Array
Now, we have a clear understanding of lists and arrays in Python. Let’s understand the differences between them:
|
List |
Array |
Accessibility |
The list is an in-built data structure |
The array is not an in-built data structure, and we need to import array or numpy to use |
Type of Data |
Lists support the storage of varied data |
Arrays limit the storage to a single data type at a time |
Memory Efficiency |
Lists are not memory efficient and have additional memory overhead |
Arrays are memory-efficient |
Performance |
Lists exhibit slower performance |
Arrays have better performance |
Flexibility |
Lists are highly flexible and support a wider range of operations |
Arrays are not as flexible as lists and support a limited set of operation |
Arithmetic Operations |
Lists don’t handle arithmetic operations directly |
Arrays support arithmetic operations directly |
Number of Elements |
The list is preferred for a smaller number of elements |
The array is preferred for a larger number of elements |
Nesting |
Nested lists can be of varied size |
Nested arrays can only support same-sized arrays |
Conclusion
In this article, we started by looking at lists in Python. We understood the basic concepts related to lists and then later looked at some examples to get a practical understanding. Later, we have gone through arrays using examples to get a basic understanding of them. In the end, we have discussed the differences between a list and an array. We analysed them from different criteria: usability, data type, memory usage, speed, adaptability, and modularity to name a few.
FAQs
List is one of the data structures of Python which is used for storing the data based on sequential nature and it can store the different types of data.
In Python, an array is a data structure that is utilised to store a set of elements of a similar type near a memory region.
The list is a built-in data structure in Python and can be readily accessed using square brackets ([]).
In Python, there is no in-built data structure called array, but there are libraries like array or numpy where we can use arrays in Python.
The array should be used to store data of similar data types as it provides better performance and memory efficiency.