Difference between tuple and list in python
Learn more
In Python, both tuples and lists are used to store a collection of values.
Mutability
Lists are mutable, which means you can add, remove, or modify elements in a list after it has been created.
Mutability
Tuples, on the other hand, are immutable, which means you cannot modify their elements after they have been created.
Syntax
Lists are defined using square brackets []
Tuples are defined using parentheses ()
Performance
Tuples are generally more lightweight and faster than lists, because they are immutable and can be optimized by the interpreter.
Usage
Lists are typically used to store collections
of related items that may need to be modified
Usage
Tuples are often used to represent fixed collections of related items that should not be modified.
#Creating a list
my_list = [1, 2, 3, 4]
Sample Code
# Creating a tuple
my_tuple = (1, 2, 3, 4)