Arrays vs Lists in Python
Arrays and Lists are both data structures in python used to store data. But a lot of people find the two confusing, as both of them look similar. Although, they do not serve the same purpose in python.
Both arrays and lists can be used to store elements, both can be indexed, sliced and iterated through,both are mutable objects. However, the similarities are pretty much it.
The difference between the lists and arrays are mostly the functions you can perform on them.
Lists are built into python. Basically, lists are a collection of things. They are the efficient way of storing data. You can create a list of anything, with any data type like
a = [1, 2.0, “hello”]. You will see that the type of
a is list.
Arrays in python can be imported from the array module or from the numpy package. Both are shown in the below figure.
Lists and ndarray both support having elements of different data structure. Whereas, the arrays from array module supports data only of the same data type.
Now if you want to do numerical operations on your data, your best choice is arrays. Because ,if you try to do mathematical operations on individual elements of a list you shall definitely get an error. Here is an example for it.
Numpy is actually a library for Python supporting large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
It just takes one extra step for import numpy and use it. If you don’t have numpy installed already, Install it using
pip install numpy and run the code in command line.
Thank you for reading. Hope this article was helpful to you. If you liked what you read tap/press the clap button. Also share your thoughts in the response section below:)