Get startedGet started for free

What is the difference between a NumPy array and a list?

1. What is the difference between a NumPy array and a list?

Congratulations on finishing the third chapter! In this final chapter, we'll cover some common questions about scientific computing in Python. We'll start with NumPy arrays and compare them to Python lists.

2. NumPy array

First of all, what is a NumPy array? It is a special data structure from the NumPy module representing a fundamental package for scientific computing with Python. The easiest way to create an array is to pass a list of values to the array() constructor. At the first glance, there isn't a big difference to Python's native lists.

3. Similarities between an array and a list

Both data structures are Iterables.

4. Similarities between an array and a list

In both cases we can use indexing to access elements.

5. Similarities between an array and a list

Moreover, NumPy arrays and Python lists can be modified similarly. What's so special about NumPy arrays then?

6. Difference between an array an a list

Compared to lists, NumPy arrays are optimized for high efficiency computations. How? First of all, NumPy arrays only store data of the same type.

7. .dtype property

When we have an array, we can retrieve the data type it stores by accessing its .dtype property. In this case, our array stores integers represented by 64 bits.

8. Changing the data type of an element

Compared to lists, if we try to modify an element with a different data type, we'll get ValueError.

9. Specifying the data type explicitly

Actually, we can explicitly specify the data type when we create an array using the dtype keyword argument.

10. Specifying the data type explicitly

Independently from the list we pass, we can specify other data types like a string, for example. The output we see here means a one-character string.

11. Object as a data type

If we want an array to behave like a list with respect to modification, we can use the dtype equal to 'O' which stands for Object. In this case, we can mix data types. However, we also limit the set of operations we can apply to such an array.

12. Difference between an array and a list

The second property of NumPy arrays is that they offer a special way to access their elements.

13. Accessing items

Let's assume we have this two-dimensional list. As an array it can be defined like this. To retrieve a single item, say the 8 in this case, both lists and arrays provide similar options.

14. Accessing items

With arrays though, it's not necessary to specify additional square brackets.

15. Accessing items

But how do we retrieve an entire data block?

16. Accessing items

The solution for a list can be tricky.

17. Accessing items

An array provides a more elegant and efficient way via slicing.

18. Difference between an array and a list

Third, operations work differently on arrays. For simplicity, we'll focus only on numeric arrays.

19. Operations +, -, *, / with lists

Let's recall that, given two lists, most of the simple mathematical operations will result in TypeError. Addition is an exception; it concatenates given lists.

20. Operations +, -, *, / with arrays

In case of NumPy arrays, operations are performed element-wise. As a result, we get a new array.

21. Operations +, -, *, / with multidimensional arrays

The same applies to multidimensional arrays.

22. Conditional operations

Conditional operations are especially useful. Applying them on an array returns a new array of booleans indicating whether the condition is satisfied or not. The cool part is that we can use these conditions to filter our arrays. This operation takes much more effort with lists.

23. Broadcasting

Another important feature of arrays is broadcasting. It describes how operations work on arrays of different dimensions. For example, what happens if we multiply this array by 3? We certainly know the answer for lists: they get extended. In case of arrays, each element is multiplied by 3 resulting in a new array. The same applies to other operations. We say that 3 broadcasts itself to all the array elements meaning that 3 operates on each element separately.

24. Broadcasting with multidimensional arrays

We can do broadcasting with multidimensional arrays. For this example, the one-dimensional array broadcasts itself to all three rows of the two-dimensional array. Broadcasting is applied to rows by default. If we want to broadcast to columns,

25. Broadcasting with multidimensional arrays

we need to modify our one-dimensional array to be a column vector. And here's the result.

26. Let's practice

Let's practice our knowledge on NumPy arrays now!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.