# Difference Between List & NumPy Array in Python

Hi! This short tutorial examines the difference between lists and NumPy arrays in the Python programming language.

First, though, here is a quick overview:

Let’s dive into the discussion and Python code!

## Python List

A Python list is used to store different data types, like strings, integers, in a variable. Python lists are mutable, which means they can be altered or modified. Below are examples of Python lists containing integers, floats, strings, a mixture of them, and user-defined objects from top to bottom:

```# list of integers
int_list = [5,4,9,8,1,6,2]
print(int_list)

# [5, 4, 9, 8, 1, 6, 2]

# list of floats
float_list = [0.98,1.57,8.71,3.54,7.12]
print(float_list)

# [0.98, 1.57, 8.71, 3.54, 7.12]

# list of strings
str_list = ["The", "sun", "shines", "bright", "everyday"]
print(str_list)

# ['The', 'sun', 'shines', 'bright', 'everyday']

# list of mixed data types
mixed_list = [5.21,4.83,2.18,"house","car",3,2,1]
print(mixed_list)

# [5.21, 4.83, 2.18, 'house', 'car', 3, 2, 1]

# list of object
my_countries = []
class Country:
def __init__(self,country,capital):
self.country = country
self.capital = capital

my_countries.append(Country("Japan", "Tokyo"))
my_countries.append(Country("France", "Paris"))
my_countries.append(Country("China", "Beijing"))
my_countries.append(Country("Germany", "Berlin"))
my_countries.append(Country("Netherlands", "Amsterdam"))
my_countries.append(Country("Nigeria", "Abuja"))

for i in my_countries:
print(f"{i.country} - {i.capital}")

# Japan - Tokyo
# France - Paris
# China - Beijing
# Germany - Berlin
# Netherlands - Amsterdam
# Nigeria - Abuja```

We can also create a Python list using the list() function:

```# list of integers
int_list = list((5,4,9,8,1,6,2))
print(int_list)

# [5, 4, 9, 8, 1, 6, 2]

# list of floats
float_list = list((0.98,1.57,8.71,3.54,7.12))
print(float_list)

# [0.98, 1.57, 8.71, 3.54, 7.12]

# list of mixed data types
mixed_list = list((5.21,4.83,2.18,"house","car",3,2,1))
print(mixed_list)

# [5.21, 4.83, 2.18, 'house', 'car', 3, 2, 1]

# list of strings
str_list = list(("The", "sun", "shines", "bright", "everyday"))
print(str_list)

# ['The', 'sun', 'shines', 'bright', 'everyday']```

In the examples above, we parsed a tuple to the list constructor, which returned a list of integers, strings, floats and mix of data them.

## Attributes of Python list

Below are the attributes of Python list:

• A list is an ordered collection of elements, where each element has a specific index starting from 0.
• Lists are mutable, meaning you can add, remove, or modify elements after creating the list.
• The elements in a list can be accessed by their index values.
• The indexes of a list are always integers.
• A list can hold duplicate values.

## NumPy Array

The NumPy Python library is the foremost library in Python for numerical and scientific computations. NumPy provides an array object similar to the built-in Python list; however, the elements of an array are typically homogeneous, meaning they can only be of the same data type, whereas lists can contain elements of different data types.

To use NumPy, it has to, first of all, be installed and imported into our Python environment. Therefore, run the lines of code below in your Python IDE to install and import NumPy:

```# install NumPy
pip install numpy

# import NumPy
import numpy as np```

With NumPy installed and imported, we can now take a look at example arrays:

```# integer array
int_array = np.array([1,2,3,4,5,6,7])
print(int_array)

# [1 2 3 4 5 6 7]

# float array
float_array = np.array([1.2,2.3,3.4,4.5,5.6,6.7])
print(float_array)

# [1.2 2.3 3.4 4.5 5.6 6.7]```

The np.array() function typically takes a Python list, and converts it to an array.

## Attributes of NumPy Array

Below are the attributes of NumPy array:

• All elements in a NumPy array must be of the same data type. This is different from Python lists, which can contain elements of different data types.
• The size of a NumPy array is fixed when it is created and cannot be changed. This means that you cannot add or remove elements from a NumPy array like you can with a Python list.
• NumPy arrays are designed for fast mathematical operations, which makes them ideal for numerical computing tasks.
• NumPy arrays can be sliced and indexed like Python lists, allowing you to extract and modify subsets of the data.
• NumPy arrays are more memory-efficient than Python lists, which is important when working with large datasets.
• NumPy arrays can be easily converted to and from other data types, such as Pandas DataFrames and Python lists. This makes it easy to use NumPy with other Python libraries.

So, while lists allow flexibility and ease of use, NumPy arrays provide optimized mathematical operations and memory efficiency, making them ideal for numerical computing tasks. Combining the two provides a versatile and efficient toolset for data analysis in Python.

We have examined the difference between lists and NumPy arrays in Python. I hope you found this tutorial helpful!

## Video, Further Resources & Summary

Do you need more explanations on the difference between lists and NumPy arrays in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail the difference between lists and NumPy arrays in Python.

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown the difference between lists and NumPy arrays in Python. In case you have further questions, you may leave a comment below.

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

Subscribe to the Statistics Globe Newsletter