List And Tuple | What Is The Difference Between List And Tuple?

In this Tutorial, we will learn The Difference Between a List And Tuple. Here there are four different differences between tuples and lists that are mutability, syntax, usage, and performance we will discuss them one by one.

List And Tuple

List And Tuple

As we know a list and a tuple are two different data structures which used to store data in different ways on different ways that we will learn here.

Syntax

There are different syntaxes for both of them to declare and store the data where we use square brackets to store list data whereas for tuples we use small brackets let’s look at the example to declare both the data structure.

# Creating a list
fruits_list = ['apple', 'banana', 'orange']

# Creating a tuple
fruits_tuple = ('apple', 'banana', 'orange')

# Updating the list
fruits_list[1] = 'grape'

# Trying to update the tuple - this will raise a TypeError
fruits_tuple[1] = 'grape'

Here we can see how both list and tuple are declared and for updating also we can see how we try to update the data there in the case of the list it was done as it is mutable whereas in the case of the tuple, it was not possible and it shows the error during the updation of the tuple.

Mutability

It is a property of data structure that deals with the updation of any of the elements after the declaration and mutable are the data structures that can be edited as example lists but immutable like tuples can not be edited and that we can easily see in the example given above.

Usage

Lists are used when we want to store data that might be required to edit frequently where data stored once might not be the final data whereas in the case of the tuple, we store the data which we do not want to edit or we can say we do not want to edited not even by mistake which is the most important difference in between two.

Performance

Here there are differences in the performance of both where both are having different storage and performance by taking memory as tuple take less memory as compare to list so it is faster.

 

To learn more about tuples and lists at List vs tuple.

Also visit Some Basic Python Tuple Programs-3 and to learn more about different programming languages visit beta Python Programming Languages tutorials

Leave a Comment

%d bloggers like this: