
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Python List and Array
In this article we are going to discuss about the difference between Python list and array. As you may know both of these are ways to store a collection of items like a bunch of numbers or words but they are not exactly the same. So understanding how they are different will help you select the right one when you are writing your program.
List
Lists are one of the four most commonly used data structures provided by Python. A list is a data structure in python that is mutable and has an ordered sequence of elements. Lists also support negative indexing.
Example
Following is a list of integer values:
# Creating a list lis= [1,2,3,4,5] # Printing the list print(lis)
Output
When you run the program, it will show this output-
[1, 2, 3, 4, 5]
Array
An array is a data structure that holds data in a linear format. Arrays hold a fixed number of elements and these elements should be homogenous (have the same data type). It is also square bracketed, ordered, mutable, and ordered.
Declaring an array by importing the array module.
In Python, we have to import an array module or import NumPy to declare an array.
Example
In this example, we will declare an array by importing the array module.
# Importing the array module import array as arr # Declaring an array using the array module sample_array = arr.array("i", [1, 2, 3, 4]) # Printing the array print(sample_array) print(type(sample_array))
Output
After running the program, you will get this result-
array('i', [1, 2, 3, 4]) <type 'array.array'>
Declaring an array by importing Numpy
In this example, we will declare an array by importing the numpy module.
# Importing the numpy module import numpy as np # Declaring an array using numpy sample_array = np.array([1, 2, 3, 4]) # Printing the array print(sample_array) print(type(sample_array))
Output
The above code produces the following results-
[1 2 3 4] <class 'numpy.ndarray'>
Examples to Show the Difference
Here are some of the examples to show the difference between a list and an array in python:
Example 1
In this example, we will show the difference between a list and an array in python.
# Importing the array module import array as arr # Importing the numpy module import numpy as np # Declaring a list sample_list = [1, 2, 3, 4] # Declaring an array using the array module sample_array = arr.array("i", [1, 2, 3, 4]) # Declaring an array using numpy sample_array_numpy = np.array([1, 2, 3, 4]) # Printing the list print("List: ", sample_list) # Printing the array print("Array using array module: ", sample_array) # Printing the array using numpy print("Array using numpy: ", sample_array_numpy)
Output
This will create the below outcome:
List: [1, 2, 3, 4] Array using array module: array('i', [1, 2, 3, 4]) Array using numpy: [1 2 3 4]
Example 2
In this example, we will declare a list and an array and we will sum the values of list and array separately
my_list = [10, 20, 30] total_list = sum(my_list) print("Sum of list:", total_list) import array numbers = array.array('i', [5, 10, 15]) total_array = sum(numbers) print("Sum of array:", total_array)
Output
This will generate the below result:
Sum of list: 60 Sum of array: 30
Differences between a list and an array in python
The following are a few important differences between a list and an array in python -
List | Array |
---|---|
Lists are heterogeneous(they can store values of different data types). | Arrays are homogenous( they can only store values of the same data type). |
There is no requirement for importing any module to declare a list. | We need to import module explicitly to declare an array. |
Lists cannot handle arithmetic operations | Arrays can handle arithmetic operations. |
List consumes large memory when compared to an array | Arrays are more compact in memory size comparatively list. |
Modifications to data items such as insertion, deletion, and update are simple. | It is difficult to modify an array since addition, deletion, and update operation is performed on a single element at a time. |
It can be nested to hold several types of components. | All nested components must be the same size. |
We can print the entire list with the help of explicit looping. | We can print the entire list without the help of explicit looping. |
Lists are preferred for short data sequences. | Arrays are preferred for longer data sequences. |