Difference Between List and Tuple in Python with Example.

Difference Between List and Tuple

List and Tuple are the two most commonly used data structures in Python programming. They are used for storing objects of any type in python. In this article, we are going to discuss the difference between List and Tuple and their use cases with examples. 


What is a List in Python?

In Python, a list is a built-in data structure that represents an ordered collection of items or elements. It is a mutable sequence, which means you can change its content, and it can contain any type of data such as numbers, strings, or other objects. 

Example:

fruits = ['apple', 'banana', 'orange', 'kiwi']


What is a Tuple in Python?

A tuple is a built-in data structure that represents an ordered collection of elements, similar to a list. However, unlike lists, tuples are immutable, which means that once a tuple is created, you cannot add, remove, or modify its elements.

Example:

my_tuple = (1, 'hello', 3.14)


Here is the important difference between List and Tuple:


1. Mutability: The list is mutable, which means you can add, remove or modify items after the list is created. On the other hand, tuples are immutable, which means that once the tuple is created, its content cannot be changed.

Example to show mutability of List and Tuple.

#creating a list and a tuple
my_list = [1, 2, 3]
my_tuple = (1, 2, 3)

#changing the first element of the list
my_list[0] = 4

#trying to change the first element of the tuple will result in an error
my_tuple[0] = 4

print(my_list) #output: [4, 2, 3]
print(my_tuple) #output: TypeError: 'tuple' object does not support item assignment
Output:

2. Speed: The list is dynamic in nature, whereas the tuple is static in nature, and because of this tuple is faster than the list.


3. Syntax: A list is defined using square brackets [] while a tuple is defined using parentheses ().


4. UsageLists are typically used for storing collections of items that can be changed throughout the program. Tuples, on the other hand, are generally used for storing collections of items that should not be changed during the program's execution, such as a set of coordinates or a date.

Example Code:

#creating a list of days in a week
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']

#creating a tuple of coordinates
coordinates = (10.0, 20.0)

#trying to change an item in weekdays will work
weekdays[2] = 'Woden-day'

#trying to change an item in coordinates will result in an error
coordinates[0] = 20.0

print(weekdays) #output: ['Monday', 'Tuesday', 'Woden-day', 'Thursday', 'Friday']
print(coordinates) #output: TypeError: 'tuple' object does not support item assignment

In this example, we can see that we changed an item in the weekday list successfully but trying to change an item in the coordinates tuple resulted in a TypeError.

Difference Between List and Tuple.

Python List Python Tuple
Lists are Mutable in nature. Tuples are immutable in nature.
Square brackets [] are used to define the List Parentheses () are used to define Tuples.
Iteration over a List is time-consuming. Iteration over Tuple is faster.
Insertion and Deletion operations are performed better on List. Accessing an element of Tuple is more appropriate.
List Consume more memory. Tuple Consumes less memory.
The list has many built-in functions. Tuple has very few built-in functions.

Lists are typically used for collections of items that can be changed, while tuples are used for collections of items that should not be changed.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS