Difference among list, tuple and set in Python and when to use each one
Tags: python, datastructure, list, set
Published: 2019-05-28 09:15:08

---
title: Difference among list, tuple and set in Python and when to use each one
published: true
tags: python,data structure,list,set
canonical_url: https://flaviabastos.ca/2019/05/28/difference-among-list-tuple-and-set-in-python-and-when-to-use-each-one/
---

Python has three similar data structures that can hold an unordered collection of elements: list, tuple and set.

Here’s how they differ:

## initialization

**list** : using the built-in `list()` or the square brackets (`[]`):

```
list1 = list() # creates an empty listlist2 = [] # also creates an empty listlist3 = ['a', 'b', 'c', 4]
```

**tuple:** elements separated by comma:

```
t = 'one', 'two', 'three', 4
```

**set** : using the built-in `set()` or curly braces (`{}`):

```
set1 = set() # creates an empty setset2 = {'one', 'two', 'three', 4}
```

## access

Access **list** and **tuple** elements using their index:

```
>>> list3[0]'a'>>> t[0]'one'
```

A **set** cannot be accessed via index:

```
>>> set2[0]Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'set' object does not support indexing
```

## mutability

A **list** is mutable:

```
>>> list3[0] = 1>>> list3[1, 'b', 'c', 4]
```

A **tuple** is immutable:

```
>>> t[0] = 1Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'tuple' object does not support item assignment
```

**set** s are also immutable since you cannot access its elements through index.

## basic usage

**list** : used to store a collection of homogeneous data that can be mutated (but can also be used to store heterogeneous data as in my `list3` example above!). **list** is very versatile and implements all [common](https://docs.python.org/3.7/library/stdtypes.html#typesseq-common) and [mutable](https://docs.python.org/3.7/library/stdtypes.html#typesseq-mutable) sequence operators. They can also be used as [stacks](https://docs.python.org/3.7/tutorial/datastructures.html#using-lists-as-stacks) and [queues](https://docs.python.org/3.7/tutorial/datastructures.html#using-lists-as-queues).

**tuple:** used to store a collection of heterogeneous data, like in my example `t` above, or to store homogeneous data that cannot be mutated. They are used to create the output of the [enumerate()](https://docs.python.org/3.7/library/functions.html#enumerate) built-in:

```
>>> doggos = ['labrador', 'german shepherd', 'poodle'] # regular list>>> list(enumerate(doggos))[(0, 'labrador'), (1, 'german shepherd'), (2, 'poodle')] # list of tuples, where the first element in the tuple is the index# and the second is the value from the list
```

> I wrote more about using `enumerate()` to map a string [here](https://dev.to/flaviabastos/map-a-string-in-python-with-enumerate-34kg).

**set:** used to eliminate duplicate elements and for membership testing:

```
>>> 4 in set3 # membership testingTrue>>> set4 = set('banana') # eliminate duplicates>>> set4{'a', 'b', 'n'}>>> set5 = {'one', 'two', 'three', 'one', 'three'} >>> set5{'three', 'one', 'two'}>>> set6 = set(['one', 'two', 'three', 'two', 'one'])>>> set6{'three', 'one', 'two'}
```

> _The post_ [Difference among list, tuple and set in Python and when to use each one](https://wp.me/pa0b0y-3V)_ was originally published on _[flaviabastos.ca](https://flaviabastos.ca/)