DEV Community

Prajeen
Prajeen

Posted on • Originally published at breakdowndata.com on

Why is Tuple faster than List and when to use List

Let us make some inroads into uses, advantage of lists and tuples

Photo by Julia Joppien on Unsplash

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.

myList = ['mango', 'apple', 'orange']
Enter fullscreen mode Exit fullscreen mode

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.

myTuple = ('Red', 'Black', 'White')
Enter fullscreen mode Exit fullscreen mode

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’.
#schema of tuple => (person name, age, weight)
StudentDetails = ('Job John', 35, 72)
Enter fullscreen mode Exit fullscreen mode
  • 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?

Top comments (0)