DEV Community

Max
Max

Posted on

Python list vs tuple

In this tutorial, we will learn about the difference between python lists and tuples.

Python Lists and tuples are used to store series of values and objects in a ordered way. Values can accessed using the index.

What is mutable data type

In python, mutable data types are whose values can be changed after they are declared. List, Dictionary and Set are mutable data type.

What is immutable data type

In Python, immutable data types are whose values cannot be changed once they are declared. Python Tuple, Integer, Float, String are immutable data type.

List syntax

List is declared by using [] square bracket

l = [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Tuple syntax

Tuple is declared by using () bracket

t = (1, 2, 3, 4, 5)
Enter fullscreen mode Exit fullscreen mode

Accessing the values inside list and tuple using index

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

t = (1, 2, 3, 4, 5)
print(t[0])
Enter fullscreen mode Exit fullscreen mode

Output

1
1
Enter fullscreen mode Exit fullscreen mode

Accessing the values inside list and tuple using slicing

l = [1, 2, 3, 4, 5]
print(l[0:2])

t = (1, 2, 3, 4, 5)
print(t[0:2])
Enter fullscreen mode Exit fullscreen mode

Output

[1, 2]
(1, 2)
Enter fullscreen mode Exit fullscreen mode

Update the values inside list and tuple

l = [1, 2, 3, 4, 5]

l[0] = 100
print(l)

t = (1, 2, 3, 4, 5)

t[0] = 100
print(t)
Enter fullscreen mode Exit fullscreen mode

Output

[100, 2, 3, 4, 5]
Traceback (most recent call last):
File "<string>", line 6, in <module>
TypeError: 'tuple' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode

You can see in the above output, value update to the list is working find but when updating a value in tuple it throws an exception error says that 'tuple' object does not support item assignment.

Python update a list value inside a tuple

t = ([1, 2, 3, 4, 5], 2, 3, 4, 5)

t[0][1] = 200
print(t)
Enter fullscreen mode Exit fullscreen mode

Output

([1, 200, 3, 4, 5], 2, 3, 4, 5)
Enter fullscreen mode Exit fullscreen mode

You can see in the above example output, python will allow to update a list value inside a tuple, because the inner value is under list data type which is mutable.

But it will not allow to update the entire value.

t = ([1, 2, 3, 4, 5], 2, 3, 4, 5)
t[0] = 100
print(t)
Enter fullscreen mode Exit fullscreen mode

Output

Traceback (most recent call last):
  File "<string>", line 2, in <module>
TypeError: 'tuple' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode
Python List Python Tuple
Use square brackets [] Use parentheses ()
List is mutable Tuple is immutable
List use more memory Tuple use less memory compared to list
List will take more time while iteration Tuple will take less time while iteration compared to list
Build in method support No Build in method support
Can contain different data type values inside Can contain different data type values inside

Explore Other Dev.to Articles

Python dictionary append
Python One Line While Loop
Read and write csv file in Python
Python Install Jupyter Notebook
Python Create Virtual Environment
Read and Write JSON in Python Requests from API

Top comments (0)