List v/s Tuple

 
What is a List?
In other programming languages, list objects are declared similarly to arrays. Lists don't have to be homogeneous all the time, so they can simultaneously store items of different data types. This makes lists the most useful tool. The list is a kind of container data Structure of Python that is used to hold numerous pieces of data simultaneously. Lists are helpful when we need to iterate over some elements and keep hold of the items.


What is a Tuple?
A tuple is another data structure to store the collection of items of many data types, but unlike mutable lists, tuples are immutable. A tuple, in other words, is a collection of items separated by commas. Because of its static structure, the tuple is more efficient than the list.
Program




Program
  
list_ = [4, 5, 7, 1, 7]  
tuple_ = (4, 1, 8, 3, 9)  
  
print("List is: ", list_)  
print("Tuple is: ", tuple_)
print("                           ")


# Code to print the data type of the data structure using the type() function  
print( type(list_) )  
print( type(tuple_) )
print("                           ")
print("================================================")


# Updating the element of list and tuple at a particular index  
# creating a list and a tuple 

list_ = ["Python", "Lists", "Tuples", "Differences"]
print(list_)
print("                           ")

tuple_ = ("Python", "Lists", "Tuples", "Differences")
print(tuple_)
print("                           ")


# modifying the last string in both data structures  
list_[3] = "Mutable"  
print( list_ )  
try:  
    tuple_[3] = "Immutable"  
    print( tuple_ )  
except TypeError:  
    print( "Tuples cannot be modified because they are immutable" )
print("================================================")


# Code to show the difference in the size of a list and a tuple  
  
#creating a list and a tuple  
list_ = ["Python", "Lists", "Tuples", "Differences"]  
tuple_ = ("Python", "Lists", "Tuples", "Differences") 

# printing sizes   
print("Size of tuple: ", tuple_.__sizeof__())  
print("Size of list: ", list_.__sizeof__())


Output:

List is:  [4, 5, 7, 1, 7]
Tuple is:  (4, 1, 8, 3, 9)
                           
<class 'list'>
<class 'tuple'>
                           
================================================
['Python', 'Lists', 'Tuples', 'Differences']
                           
('Python', 'Lists', 'Tuples', 'Differences')
                           
['Python', 'Lists', 'Tuples', 'Mutable']
Tuples cannot be modified because they are immutable
================================================
Size of tuple:  56
Size of list:  72




Tuples and Lists: Key Similarities
  • They both hold collections of items and are heterogeneous data types, meaning they can contain multiple data types simultaneously.
  • They're both ordered, which implies the items or objects are maintained in the same order as they were placed until changed manually.
  • Because they're both sequential data structures, we can iterate through the objects they hold; hence, they are iterables.
  • An integer index, enclosed in square brackets [index], can be used to access objects of both data types.