• Register
Difference between lists and arrays in python

Arrays and lists are both used in Python to store data, but they don't serve exactly the same purposes. They both can be used to store any data type (real numbers, strings, etc), and they both can be indexed and iterated through, but the similarities between the two don't go much further. The main difference between a list and an array is the functions that you can perform to them. For example, you can divide an array by 3, and each number in the array will be divided by 3 and the result will be printed if you request it. If you try to divide a list by 3, Python will tell you that it can't be done, and an error will be thrown.


Here's how it would work:

x = array([3, 6, 9, 12])

In the above example, your output would be:

array([1, 2, 3, 4])

If you tried to do the same with a list, it would very similar:

y = [3, 6, 9, 12]

It's almost exactly like the first example, except you wouldn't get a valid output because the code would throw an error.

It does take an extra step to use arrays because they have to be declared while lists don't because they are part of Python's syntax, so lists are generally used more often between the two, which works fine most of the time. However, if you're going to perform arithmetic functions to your lists, you should really be using arrays instead. Additionally, arrays will store your data more compactly and efficiently, so if you're storing a large amount of data, you may consider using arrays as well.

You can think of it that way but Python’s list allows you to store objects of different types within it and it also grows dynamically. More memory is allocated when required. An actual array usually only allows you to store one kind of datatype within it and it’s a fixed size in terms of memory allocation.

There’s an array module in python that actually allows you to store certain types of data (I think only number types, could be wrong) but I don’t think it’s that useful because most of the time, you’ll have complex object types that you’ll want to store in an array for the array and it’s benefits to be useful to you.

You can think of a list type as using an array behind the scenes. It’s allocating a fixed block of memory to store whatever you wish to store. When you keep adding data, it eventually runs out of space to store more elements. A new array is created behind the scenes that’s bigger in size than the current full array, and elements get copied from the current array to the new array with some extra space left over to add more objects.

From a Python perspective, you don’t see any of this happening. You just use the append method or whatever as much as you want and the list happily stores your data.

Of course, I don’t know the actual implementation details of python’s list but it’s definitely implemented as a dynamic array behind the scenes, so you can think of it this way.

So no, Python’s list isn’t an array by definition but it’s using an array structure internally for its functionality.

1 Comment

You never know what you will get on the internet. It is full of surprises like this amazing blog post.

More Posts

How are python dictionaries different from python lists? Hafsa_Rao - Jun 28
Difference between pop and remove in python Sanjana Sagar - May 20
Python difference between = and == Sanjana Sagar - May 27
4. What is the Difference Between is and == in Python? hubconcepts - Oct 21, 2020
Different between list and array in python Tania - May 8
Difference between ' and " in python amna - Mar 16
Python check if two lists are equal IamAdinath - Jan 28
Variable sized arrays in C++ sakshi - Jul 11
Comparing two arrays in javascript returning differences Sanjana Sagar - May 19
How do we use for-each loop in arrays muliemes - May 18