0% found this document useful (0 votes)
40 views

Difference Between List and Array in Python

Uploaded by

ak.edu.school
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Difference Between List and Array in Python

Uploaded by

ak.edu.school
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Difference between List and Array in Python

geeksforgeeks.org/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.

Python3

1/4
# 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.

Python3

# 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

2/4
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 Only consists of elements belonging to the


different data types same data type

No need to explicitly import a module for Need to explicitly import the array module
the declaration for declaration

Cannot directly handle arithmetic Can directly handle arithmetic operations


operations

Preferred for a shorter sequence of data Preferred for a longer sequence of data
items items

Greater flexibility allows easy Less flexibility since addition, and deletion
modification (addition, deletion) of data has to be done element-wise

The entire list can be printed without any A loop has to be formed to print or access
explicit looping the components of the array

Consume larger memory for easy Comparatively more compact in memory


addition of elements size

Nested lists can be of variable size Nested arrays has to be of same size.

3/4
List Array

Can perform direct operations using Need to import proper modules to perform
functions like: these operations.
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…

Example: Example:
my_list = [1, 2, 3, 4] import array
arr = array.array(‘i’, [1, 2, 3])

4/4

You might also like