Motivation
Never really took the time to clearly distinguish the differences between the two. Here we go.
Main difference
- An array stores values of the same data type.
- A list can store values of different data types.
Other differences/features of arrays
- In order to use arrays in Python, you have to import the array module. Reason? It is not a fundamental data type.
- Declaring an array looks something like this:
1
2from array import *
my_array = array('i', [1,2,3])
Notice that it does not look as simple as declaring a list. Here's why.
Syntax of declaring array
1 | from array import * |
The typecode tells python the type of array. There are 13 typecodes altogether.
Similarities (yes!)
There are many built-in methods that be used on BOTH lists and arrays. To name a few (full list of methods can be seen here):
append()
pop()
sort()
Conclusion
Just don't use arrays in Python if you don't have to.