Difference between List and Array in Python
In Python, lists and arrays are the data structures that are used to store multiple items. They both support the indexing of elements to access them, slicing, and iterating over the elements. In this article, we will see the difference between the two.
Operations Difference in Lists and Arrays
Accessing element is fast in Python Arrays because they are in a contiguous manner but insertion and deletion is quite expensive because all the elements are shifted from the position of inserting and deleting element linearly. Suppose the array is of 1000 length and we are inserting/deleting elements at 100 position then all the elements after the hundred position will get shifted due to which the operation becomes expensive.
Accessing an element in a Python List is the same as an Array because a List is actually a dynamic array . Inserting or deleting elements at the beginning or in the middle of a list can be less efficient because it may require shifting all subsequent elements, which is a linear-time operation in the worst case.
What are Lists?
A list in Python is an inbuilt collection of items that can contain elements of multiple data types, which may be either numeric, character logical values, etc. It is an ordered collection supporting negative indexing. A list can be created using [] containing data values. Contents of lists can be easily merged and copied using Python’s inbuilt functions.
Example:
In this example, we are creating a list in Python. The first element of the list is an integer, the second a Python string, and the third is a list of characters.
# creating a list containing elements
# belonging to different data types
sample_list = [1, "Yash", ['a', 'e']]
print(type(sample_list))
print(sample_list)
Output:
<class 'list'>
[1, 'Yash', ['a', 'e']]
What are Arrays?
An array is a vector containing homogeneous elements i.e. belonging to the same data type. Elements are allocated with contiguous memory locations. Typically the size of an array is fixed. Th e insertion and deletion costs are high as compared to the list however indexing is faster in the Arrays due to contiguous memory allocation. Arrays can be used by importing the array module.
Example:
In this example, we will create a Python array by using the array() function of the array module and see its type using the type() function.
# importing "array" for array creations
import array as arr
# creating an array with integer type
a = arr.array('i', [1, 2, 3])
print(type(a))
for i in a:
print(i, end=" ")
Output:
<class 'array.array'>
1 2 3
Difference Between List and Array in Python
The following table shows the differences between List and Array in Python:
List | Array |
---|---|
Can consist of elements belonging to different data types | Only consists of elements belonging to the same data type |
No need to explicitly import a module for the declaration | Need to explicitly import the array module for declaration |
Cannot directly handle arithmetic operations | Can directly handle arithmetic operations |
Preferred for a shorter sequence of data items | Preferred for a longer sequence of data items |
Greater flexibility allows easy modification (addition, deletion) of data | Less flexibility since addition, and deletion has to be done element-wise |
The entire list can be printed without any explicit looping | A loop has to be formed to print or access the components of the array |
Consume larger memory for easy addition of elements | Comparatively more compact in memory size |
Nested lists can be of variable size | Nested arrays has to be of same size. |
Can perform direct operations using functions like: No need to import anything to use these functions. | Need to import proper modules to perform these operations. |
Example: my_list = [1, 2, 3, 4] | Example: import array arr = array.array(‘i’, [1, 2, 3]) |
Difference between List and Array in Python – FAQs
What is the Difference Between an Array and a List in Python?
- Arrays (when talking about the
array
module in Python) are specifically used to store a collection of numeric elements that are all of the same type. This makes them more efficient for storing large amounts of data and performing numerical computations where the type consistency is maintained.- Syntax: Need to import the
array
module to use arrays.- Example:
from array import array
arr = array('i', [1, 2, 3, 4]) # Array of integers
- Array:
- List:
- Lists are more flexible than arrays in that they can hold elements of different types (integers, strings, objects, etc.). They come built-in with Python and do not require importing any additional modules.
- Lists support a variety of operations that can modify the list.
- Example:
lst = [1, 'hello', 3.14, [1, 2, 3]]
What is the Difference Between ArrayList and Set in Python?
Python doesn’t have an
ArrayList
as it is a concept from Java. The closest equivalent in Python is alist
. Here’s how it compares to aset
:
- List:
- Ordered collection which means it maintains the order of elements as they were added.
- Can contain duplicate elements.
- Example:
- Set:
- An unordered collection of unique elements. It automatically removes any duplicate entries.
- Because it is unordered, it does not support operations that depend on sequence (like indexing).
- Example:
my_list = [1, 2, 2, 3]
my_set = {1, 2, 2, 3} # Output will be {1, 2, 3}
What is the Difference Between Array and List in Python Stack Overflow?
This refers to common questions asked on Stack Overflow about the difference between arrays and lists in Python, which typically highlight:
- Arrays are best for numeric data and require the specification of a data type.
- Lists are more general-purpose containers that are more flexible but less memory efficient for large numeric data sets.
What is the Difference Between List, Array, and Dictionary in Python?
- List: An ordered sequence of elements that can contain items of different types. Lists are versatile for handling a sequence of elements that need to be ordered and may need to be changed.
- Array: As provided by Python’s
array
module, used primarily for storing homogeneous numeric data in a compact and efficient manner.- Dictionary: An unordered collection of key-value pairs. Dictionaries are optimized for retrieving data where each element is accessed by a unique key. This is ideal for large datasets where fast lookup, insertion, and deletion of elements is required.
What is the Difference Between Remove and Pop?
- Used to remove the first occurrence of a specific value from a list.
- Does not return the removed element.
- Raises a
ValueError
if the specified value is not found.- Syntax:
my_list = [1, 2, 3, 2]
my_list.remove(2) # Removes the first '2'
- Remove:
- Pop:
- Removes an element at a specific index and returns it.
- If no index is specified,
pop()
removes and returns the last item in the list.- Raises an
IndexError
if the specified index is out of range.- Syntax:
my_list = [1, 2, 3]
removed_element = my_list.pop(1) # Removes and returns '2'These operations allow for specific element manipulation in lists, with
pop()
often being useful when implementing data structures like stacks and queues.