Python List VS Array VS Tuple
Last Updated :
09 Feb, 2021
List: A list is of an ordered collection data type that is mutable which means it can be easily modified and we can change its data values and a list can be indexed, sliced, and changed and each element can be accessed using its index value in the list. The following are the main characteristics of a List:
- The list is an ordered collection of data types.
- The list is mutable.
- List are dynamic and can contain objects of different data types.
- List elements can be accessed by index number.
Example:
Python
list = [ "mango" , "strawberry" , "orange" ,
"apple" , "banana" ]
print ( list )
print ( list [ 2 : 4 ])
list [ 1 ] = "grapes"
print ( list [ 1 ])
|
Output :
['mango', 'strawberry', 'orange', 'apple', 'banana']
['orange', 'apple']
grapes
Array: An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). The following are the main characteristics of an Array:
- An array is an ordered collection of the similar data types.
- An array is mutable.
- An array can be accessed by using its index number.
Examples:
Python
import array as arr
a = arr.array( 'i' , [ 1 , 2 , 3 ])
print ( "The new created array is : " , end = " " )
for i in range ( 0 , 3 ):
print (a[i], end = " " )
print ()
b = arr.array( 'd' , [ 2.5 , 3.2 , 3.3 ])
print ( "The new created array is : " , end = " " )
for i in range ( 0 , 3 ):
print (b[i], end = " " )
|
Output:
The new created array is : 1 2 3
The new created array is : 2.5 3.2 3.3
Tuple: A tuple is an ordered and an immutable data type which means we cannot change its values and tuples are written in round brackets. We can access tuple by referring to the index number inside the square brackets. The following are the main characteristics of a Tuple:
- Tuples are immutable and can store any type of data type.
- it is defined using ().
- it cannot be changed or replaced as it is an immutable data type.
Examples:
Python
tuple = ( "orange" , "apple" , "banana" )
print ( tuple )
print ( tuple [ 2 ])
print ( tuple [ 0 : 2 ])
|
Output :
('orange', 'apple', 'banana')
banana
('orange', 'apple')
Table of Difference between List, Array, and Tuple :
List
|
Array
|
Tuple
|
List is mutable |
Array is mutable |
Tuple is immutable |
A list is ordered collection of items |
An array is ordered collection of items |
A tuple is an ordered collection of items |
Item in the list can be changed or replaced |
Item in the array can be changed or replaced |
Item in the tuple cannot be changed or replaced |
List can store more than one data type |
Array can store only similar data types |
Tuple can store more than one data type |
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...