Why is Tuple faster than List and when to use List
Tags: tuples, tech, python, programming
Published: 2020-08-09 08:19:03

---
title: Why is Tuple faster than List and when to use List
published: true
date: 2020-07-25 17:59:55 UTC
tags: tuples,tech,python,programming
canonical_url: https://breakdowndata.com/why-is-tuple-faster-than-list-and-when-to-use-list/
---

_Let us make some inroads into uses, advantage of lists and tuples_

![](https://cdn-images-1.medium.com/max/1024/0*JqwfCJs5ozkb1gI7)<figcaption>Photo by <a href="https://unsplash.com/@vitreous_macula?utm_source=medium&amp;utm_medium=referral">Julia Joppien</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

#### What is a List

List in python is simply a collection which is ordered and changeable. Lists can contain multiple datatypes . It can be created by putting the elements between the square brackets.

```python
myList = ['mango', 'apple', 'orange']
```

#### What is a tuple

Much like list, tuple is also a collection of ordered elements that can contain elements of multiple datatypes. However, tuple is a _immutable_. This effectively means that you cannot edit or delete an element of a tuple. Tuple can be created by putting elements between round brackets.

```python
myTuple = ('Red', 'Black', 'White')
```

> There is a common perception that tuples are lists that are immutable. However it is not completely true.

#### Why should lists be used for homogeneous data and tuple for heterogeneous data?

Is this really true? is this a guideline? no.. no.. in reality both of them are heterogeneous collections. You are free to use tuples for homogeneous data and lists for heterogeneous data. It is more of a culture than a guideline.

- Main reason why list is preferred for homogeneous data is because it is mutable
- If you have list of several things of same kind, it make sense to add another one to the list or take one from it. You are still left with a list of same things

> But why is tuple different?

- If you have a set of heterogeneous elements, most probably the collection has a fixed structure or ‘schema’.

```python
#schema of tuple => (person name, age, weight)
StudentDetails = ('Job John', 35, 72)
```

- In other words, tuples can be used to store records — related information that belong together
- In such cases, tuple lets us “chunk” together related information and use it as a single entity.
- Since tuple is immutable, it can be used as key for dictionary

#### Why is tuple faster than list?

Program execution is faster when manipulating a tuple than for a list of same size. However, _it is not noticeable for collections of smaller size_.


> Whats the problem with list, anyway?

List has a method called append() to add single items to the existing list. So, for most of the append to be fast, python actually create a larger array in memory every time you create a list — in case you append.

This way when append is called, it doesn’t have to recreate the list.

Thus, making a list of five elements will cost more than five elements worth of memory.

> What does tuple do?

In contrary, since tuple is immutable, it asks for an immutable structure. This way tuples are more explicit with memory.

Thus, making a tuple of five elements will cost only five elements worth of memory.

_Finally, this overhead with memory for list costs its speed._


> If you want to define a constant set of values and the only thing you want to do with it is to iterate through them, then use tuple than a list.

For more such articles, 
[Top 10 Reasons Why 87% of the Machine Learning Projects Fail?](https://breakdowndata.com/top-10-reasons-why-87-of-machine-learning-projects-fail/)