Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Difference Between List and Tuple in Python

Last Updated on November 30, 2022 by Prepbytes

In this article, we will discuss the difference between list and tuple in Python. List and Tuple are 2 sequence-based data structures in Python. They have their own characteristics and methods. In this article, we will discuss the difference between these 2 data structures. Let us start by discussing the list data structure.

List in Python

A list is a dynamic-sized array in Python. This means that we don’t have to specify the size of the list while we create it; a list can dynamically increase or decrease its size. List in Python is just like a vector in C++ or ArrayList in Java.

However, the main difference between List and Vector (or ArrayList) is that a list can have items of multiple datatypes in it altogether.

For instance, consider the list “items” shown above, In one list, we have Strings, int numbers, and a float number.

A list is created using [ ] brackets. The program to create and print a list is shown below.

Program to Create and Print a List in Python

items = [2, 3, "apple", 89.45, "Juice"]
print(items)

A list in Python can have multiple copies of the same value i.e. duplicates are allowed in a list.

As shown in the list above, the value 1 is repeating 3 times and the values 2 and 4 are repeating twice.

As already discussed, since a list is of dynamic size, we can add and remove values from it. Consider the list shown below.

Let us say that we want to add element 10 to the end of this list. We will be able to do so by using the “append()” method as shown below.

Program to show Append Method in Lists

items = [2, 3, "apple", 89.45, "Juice"]
print(items)

items.append(10)
print(items)

After appending the value 10, the list now looks like this.

We can also insert a value at a particular index in the list. For that, we can use the “insert()” method as shown below.

Program to show Insert Method in Lists

items = [2, 3, "apple", 89.45, "Juice"]
print(items)

items.append(10)
print(items)

items.insert(2,"PrepBytes")
print(items)

So, the first parameter in the insert method is the index value and the second parameter is the value that we want to insert. As shown below, the list now has “PrepBytes” at index 2 and the values have shifted.

So, now that we have some basic knowledge of lists in Python, let us also gather some basic knowledge of tuples in Python.

Tuple in Python

A tuple in Python, just like a list, is a collection of objects. However, a tuple is created using ( ) brackets. For instance, a tuple with some data is shown below.

The program to create and print a tuple is shown below.

Program to Create and Print a Tuple in Python

items = (9, 6, 3, "Coding", "Data")
print(items)

A tuple can also have duplicate values i.e. multiple copies of the same value are allowed in a tuple.

However, there is no append() or insert() functions available for a tuple.

So, now that we have a basic idea of a list and a tuple, let us discuss the difference between them.

Difference Between List and Tuple in Python

The differences between List and Tuple in Python are listed below.

List Tuple
Lists are mutable in nature i.e. a value can be added or removed from the list. Also, an existing value can be overridden in a list. Tuples are immutable in nature. So, once a tuple is created, no new values can be added to it or no value can be removed from a tuple. Also, the existing values can’t be overridden.
When we apply an iteration on a list (i.e. traverse it using some loop), it is more time-consuming as compared to a tuple. Iterations are applied faster on a tuple as compared to a list.
A list is a better data structure for performing operations. This means that we can add/remove/update values in a list, it is more useful in cases when we require a data structure that needs to be updated frequently. A tuple is better for storing some constant values for operations. For instance, when we want to traverse a matrix in 4 adjacent directions, the row and column vector that we make in C++ or ArrayList in Java can be replaced by a tuple in Python as those values are constant.
Lists consume more memory as compared to tuples in Python. Tuples consume less memory than lists in Python.
Lists have a lot of built-in methods like insert(), append(), extend(), etc. Tuples have mainly 2 methods only count() and index().
Since a list can be updated in many ways, it is a data structure that is more prone to programming errors i.e. sometimes the programmer can see unexpected changes/errors in a list if the programmer does not use a list very carefully. Tuples are not prone to errors because of their immutable nature.

So, these are the differences between a list and a tuple in Python. Let us see the programs to showcase some of these differences practically.

Program to Show Tuples are Immutable

items = (9, 6, 3, "Coding", "Data", 3, 9)
print(items)
items[0] = 10

So, in the above program, we get an error saying that the ‘tuple object does not support item assignment’. We have already shown that lists are mutable when we used the append() and insert() functions. So, this program shows the difference in the nature of mutability and immutability in lists and tuples respectively.

Program to Show Tuples Consume Less Memory than Lists

import sys

arr1 = ["PrepBytes","Coders","Python"]
arr2 = ("PrepBytes","Coders","Python")

print(sys.getsizeof(arr1))
print(sys.getsizeof(arr2))

The above program imports the ‘sys’ module in python to get a function to know the size of a list and tuple respectively. The output is shown below.

This means that a list consumes more memory as compared to a tuple. Hence, this difference has also been proved.

So, we have listed and showcased various differences between lists and tuples in Python.

We hope that difference between list and tuple in Python is now clear to you. We also hope that you liked the complete discussion and have practiced the programs shown above yourself. Hope to see you again soon at PrepBytes.

Leave a Reply

Your email address will not be published. Required fields are marked *