What is the difference between a python list and a tuple?

PythonProgrammingServer Side Programming

List and Tuple in Python are data structures. The list is dynamic, whereas the tuple has static characteristics.

List

Lists are one of the four most used data structures provided by Python. A List is a data structure in python that is mutable and has an ordered sequence of elements. Following is a list of integer values −

lis= [1,2,3,4,5] print(lis)

If you execute the above snippet, it produces the following output −

[1, 2, 3, 4, 5]

Tuple

Tuple is a collection of python objects that are separated by commas which are ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are, that tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

tup=('tutorials', 'point', 2022,True) print(tup)

If you execute the above snippet, it produces the following output −

('tutorials', 'point', 2022, True)

In this article, we discuss the differences between lists and tuples in python.

Differences between lists and tuples

Following are the major differences between lists and tuples −

List Tuple
Lists are mutable Tuple are immutable.
Lists occupy more space. Tuples occupy less space when compared with lists.
Implementation of iterations is time taking. Implementation of iterations is comparatively faster.
Lists have many built-in functions. Tuples do not have any built-in methods.
Operations such as insertion and deletion perform better in lists. For accessing the elements, tuples perform better.
Unpredicted changes and errors occur in lists frequently. In tuples, it is hard to take place.

Mutable Vs Immutable

Lists are mutable while tuples are immutable, which means that we can modify or change the values of a list but we cannot do that in tuples.

The following example demonstrates the difference between lists and tuples in this case.

Example: List

Following example demonstrates the usage of python lists.

list = ["Meredith", "Levi", "Wright", "Franklin"] list[1]= "Kristen" print(list)

Output

['Meredith', 'Kristen', 'Wright', 'Franklin']

Example: Tuple

Now, let us see the example of tuples.

tuple = ("Meredith", "Levi", "Wright", "Franklin") tuple[1]= "Kristen" print(tuple)

Output

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    tuple[1]= "Kristen"
TypeError: 'tuple' object does not support item assignment

Memory allocation

As tuples are immutable, Python allocates larger blocks of memory with low overhead. On the other hand, python allocates small memory blocks for lists. Therefore, tuples will have less memory than the list. When you have a high number of elements, this makes tuples a little more space-efficient than lists.

Example

In the following example, we can see the difference in the space occupied for the same data.

list= ['Meredith', 'Kristen', 'Wright', 'Franklin'] tuple = ("Meredith", "Kristen", "Wright", "Franklin") print("Memory occupied by a list") print(list.__sizeof__()) print("Memory occupied by a tuple") print(tuple.__sizeof__())

Output

Memory occupied by a list
72
Memory occupied by a tuple
56

Copied Vs Reusability

Tuples can't be duplicated or copied. Because tuples cannot be changed(immutable). If you type tuple(tuple name), it will return itself right away.

Example

names = ("Meredith", "Kristen", "Wright", "Franklin") copy = tuple(names) print(names is copy)

Output

The above codes return true which means that the tuple's names and copy are the same.

True

Example 2

names = ["Meredith", "Kristen", "Wright", "Franklin"] copy = list(names) print(names is copy)

Output

As lists can be copied, the code returns false.

False
raja
Updated on 05-Sep-2022 08:50:59

Advertisements