Difference Between Tuple and List

Syntax Difference

The literal syntax of tuples is shown by parentheses {} whereas the literal syntax of lists is shown by square brackets [] .

tup_colors = ('red', 'blue', 'green') list_colors = ['red', 'blue', 'green'] print(tup_colors) print(list_colors)
('red', 'blue', 'green') ['red', 'blue', 'green']

Mutable Vs. Immutable

The key difference is that tuples are immutable. This means that you cannot change the values in a tuple once you have created it. This is a good feature to have in some data structures where you intend to not make any changes to certain parts. As a list is mutable, it can't be used as a key in a dictionary because dictionaries can use any immutable object as a key. Thus, tuples can be used as dictionary keys if needed. If you try to modify a tuple in a permitted way it becomes two Tuples: the original, which remains fo every scope other than yours, and your modified copy for your scope.

List example
colors = ['red', 'blue', 'green'] colors[1]='yellow' print(colors)

Return: ['red', 'yellow', 'green']

Tuple example
colors = ('red', 'blue', 'green') colors[1]='yellow' print(colors)


colors[1]='yellow' TypeError: 'tuple' object does not support item assignment

Reused Vs. Copied

Tuples do not need to be copied: Running tuple(some_tuple) returns immediately itself. Since tuples are immutable, they do not have to be copied:

colors = ('red', 'blue', 'green') copyColors = tuple(colors) print(colors is copyColors)


In contrast, list(some_list) requires all the data to be copied to a new list:

colors = ['red', 'blue', 'green'] copyColors = list(colors) print(colors is copyColors)


Size Comparison

Tuples operation has smaller size than that of list, which makes it a bit faster but not that much to mention about until you have a huge number of elements.

tup_colors = ('red', 'blue', 'green') list_colors = ['red', 'blue', 'green'] print(tup_colors.__sizeof__()) print(list_colors.__sizeof__())
48 64

Homogeneous Vs Heterogeneous

There's a strong culture of tuples being for heterogeneous collections, similar to what you'd use structs for in C, and lists being for homogeneous collections, similar to what you'd use arrays for. In other words, different data can be stored in single tuple while same type of data is stored in lists.

Other Differnces:

  1. Lists are for variable length , tuples are for fixed length .

  2. Tuples show structure whereas lists show order .

  3. Tuples have O(N) append, insert, and delete performance whereas Lists have O(1) append, insert and delete performance.

Looking for a Python job ?

There are lot of opportunities from many reputed companies in the world. Chances are you will need to prove that you know how to work with .Net Programming Language. These Python Interview Questions have been designed especially to get you acquainted with the nature of questions you may encounter during your interview for the subject of .Net Programming. Here's a comprehensive list of .Net Interview Questions, along with some of the best answers. These sample questions are framed by our experts team who trains for .Net training to give you an idea of type of questions which may be asked in interview. Go to... Python Interview Questions

Python tuple

A Tuple is a collection of Python objects separated by commas which is ordered and unchangeable. Python tuple is similar to List except that the objects in tuple are immutable which means we cannot change the elements of a tuple once assigned.
daysTuple = ("sunday", "monday", "tuesday")
More on..... Python Tuple

Python List

Python List vs tuple A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. They are very similar to arrays. List can contain any type of variable, and they can contain as many variables as you wish. The list type implements the sequence protocol, and also allows you to add and remove objects from the sequence. It is a mutable container. This means that we can add values, delete values, or modify existing values.
daysList = ["sunday", "monday", "tuesday"]
More on... Python List