Python list vs tuple
Tags: python, programming, tutorial, beginners
Published: 2023-03-27 16:34:31

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

[Python Lists](https://www.programdoc.com/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](https://www.programdoc.com/python/tuples), Integer, Float, String are immutable data type.

#### List syntax
List is declared by using [] square bracket
```py
l = [1, 2, 3, 4, 5]
```

#### Tuple syntax
Tuple is declared by using () bracket
```py
t = (1, 2, 3, 4, 5)
```

## Accessing the values inside list and tuple using index
```py
l = [1, 2, 3, 4, 5]
print(l[0])

t = (1, 2, 3, 4, 5)
print(t[0])
```
**Output**
```py
1
1
```

## Accessing the values inside list and tuple using slicing
```py
l = [1, 2, 3, 4, 5]
print(l[0:2])

t = (1, 2, 3, 4, 5)
print(t[0:2])
```
**Output**
```py
[1, 2]
(1, 2)
```

## Update the values inside list and tuple
```py
l = [1, 2, 3, 4, 5]

l[0] = 100
print(l)

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

t[0] = 100
print(t)
```
**Output**
```py
[100, 2, 3, 4, 5]
Traceback (most recent call last):
File "<string>", line 6, in <module>
TypeError: 'tuple' object does not support item assignment
```
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
```py
t = ([1, 2, 3, 4, 5], 2, 3, 4, 5)

t[0][1] = 200
print(t)
```
**Output**
```py
([1, 200, 3, 4, 5], 2, 3, 4, 5)
```
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.
```py
t = ([1, 2, 3, 4, 5], 2, 3, 4, 5)
t[0] = 100
print(t)
```
**Output**
```py
Traceback (most recent call last):
  File "<string>", line 2, in <module>
TypeError: 'tuple' object does not support item assignment
```


|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](https://dev.to/max24816/python-dictionary-append-1an7)
[Python One Line While Loop](https://dev.to/max24816/python-one-line-while-loop-4j2k)
[Read and write csv file in Python](https://dev.to/max24816/python-read-and-write-csv-file-2onh)
[Python Install Jupyter Notebook](https://dev.to/max24816/install-jupyter-notebook-1cm0)
[Python Create Virtual Environment](https://dev.to/max24816/python-create-virtual-environment-3iai/edit)
[Read and Write JSON in Python Requests from API](https://dev.to/max24816/read-and-write-json-in-python-requests-from-api-52ga)