Difference between Array and List in Python

In this tutorial, we will see the differences between an Array and a List in Python.

Table Of Contents

Arrays in Python

Arrays in Python are homogeneous in nature, it means a NumPy Array can store elements of same data type only. In Python, there are are two types of array i.e. a NumPy Array and an array from array module. Let’s discuss about them one by one.

NumPy Array

Numpy stands for Numeric Python. It is a Python module, designed to mathematical processing on arrays. To Use a NumPy Array, first we need to import the numpy module. Let’s see how to create an array using NumPy.

We can create a NumPy array by passing any iterable sequence to function nump.array(). It will return a NumPy Array object. For example,

import numpy as np

# Create numpy array with integers
array=np.array([1,2,3,4,5])

# Display the array
print(array)

# Get the type of array
print(type(array))

Output:

[1 2 3 4 5]
<class 'numpy.ndarray'>

Here, we created a Numpy Array, that contains five elements.

A NumPy array can contain elements of similar data type only.

array module in Python

We also have a array module in Python. Using it we can create array objects. Let’s see how to create an array using array module in python

Syntax:

import array

array.array("type",[elements])

Parameters:

It takes two parameters.

  1. The First parameter represents the datatype.
  2. The Second parameter is a sequential data structure containing elements.

Let’s create an array that holds 5 integer elements.

import array

# Create an array of integers
array=array.array("i", [1,2,3,4,5])

# Display the array
print(array)

# Get the array
print(type(array))

Output:

array('i', [1, 2, 3, 4, 5])
<class 'array.array'>

Here, we created an array, that contains five elements. An array can contain elements of similar data type only in Python.

List in Python

In Python, Lists are heterogeneous. It means a list can store elements of different data types. For example, we can create a list, that contains some integers, some floats, some strings and some other type of objects.

We can create a List in Python using list() method or [].

Let’s see an example, where we will create a list of 5 elements of different data types.

# Create a list with elements of different datatypes
listOfElements = [12, 34.6, "Welcome", 89, 'c']

print(listOfElements)

print(type(listOfElements))

output:

[12, 34.6, 'Welcome', 89, 'c']
<class 'list'>

We created a list with three different types of elements in it i.e. integer, double, and string.

Differences between an Array and a List in Python

An array can store elements of same data type only. Therefore they are homogeneous. Whereas, a list is heterogeneous, therefore it can store elements of different data types.

As array contains elements of same size only, therefore thier internal implementation is in such a way that indexing is fast in them. Therefore,

  • Selecting an element by its index position is fast in array as compared to a list.
  • Deleting & Adding elements from the middle is fast in list as compared to an array.

Summary

In this article, we have seen what an Array and a List can do in Python. We have also seen the differences between them in terms of usage and memory. Based on the application, you can use a particular data structure. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top