Last Updated : 15 Jul, 2025
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 ArraysAccessing 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.
Python
# 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'>What are Arrays?
[1, 'Yash', ['a', 'e']]
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.
Python
# 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'>Difference Between List and Array in Python
1 2 3
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:
count() - for counting a particular element in the list
sort() - sort the complete list
max() - gives maximum of the list
min() - gives minimum of the list
sum() - gives sum of all the elements in list for integer list
index() - gives first index of the element specified
append() - adds the element to the end of the list
remove() - removes the element specified
No need to import anything to use these functions.
and many more...
Need to import proper modules to perform these operations.
Example:What is the Difference Between Array and List in Python Stack Overflow?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 List, Array, and Dictionary in Python?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 Remove and Pop?
- 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.
- 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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4