Question
What are some differences between an array
and a list
?
Answer
In Python, an array, or NumPy Array, and list share many similarities, but they also have some important differences on how they can be used.
Both arrays and lists can hold multiple items of any type. You can also access individual items by indexes.
One important, and probably the main difference, between them is that you can perform operations on an array, like addition, multiplication, and subtraction, like you would a vector in mathematics.
This means that if you have an array of numbers, you can add a single number to every value in the array with one operation. With a list, operations cannot be applied on every single element like for an array, and might even cause errors.
Arrays also provide several other useful functionality, which you will learn throughout this lesson.
Example
test = np.array([1, 2, 3, 4, 5])
test += 10
# The array is now [11, 12, 13, 14, 15]