Difference Between List and Tuple

Python is a general-purpose high-level programming language. It is easy to read and learn. Therefore, it is a common language for beginners to start computer programming. Python programs are easy to test and debug. It is a language used to build a variety of applications. Some of them are machine learning, computer vision, web development, network programming. Python is used for building algorithms for solving complex problems. Two data storing methods of Python are List and Tuple. The elements of a list can be changed. So, a list is mutable. The elements of a tuple cannot be changed. So, a tuple is immutable. This article discusses the difference between list and tuple. The key difference between list and tuple is that a list is mutable while a tuple is immutable.

CONTENTS

1. Overview and Key Difference
2. What is List
3. What is Tuple
4. Similarities Between List and Tuple
5. Side by Side Comparison – List vs Tuple in Tabular Form
6. Summary

What is List?

In programming languages such as C or C++, the array is used to hold the elements of the same data type. But in Python List, all elements need not be of same time. Each item in the list is separated by a comma.  All elements are included inside square brackets. An example of a list is list1 = [1, “abc”, 4.5]; The index of a list starts with zero. Therefore, the element 1 has the index 0, and abc has the index 1 etc. It is also possible to use the negative index. The last element of the list has the index -1. Then the element “abc” has the index of -2 etc.

It is possible to take a sequence of elements from the list. This is called slicing. When there is a list as follows, which is list1 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’], the statement print(list1[2:5]) will print c,d,e. The element in index two is included but not the element in index five.

Lists are mutable. Therefore, the elements in the list can be changed. Assume that there is a list as, list1 = [2,4,6,8]. If the programmer wants to change the first element to value 1, then he can change it by writing the statement list1[0] = 1. Python language has already inbuilt functions to add new items to a list. It is the append function. When there is a list such as list1= [1,2,3], the programmer can add the new element 4 using list1.append(4).

The elements of a list can be deleted using the del () by passing the relevant index. Assume that there is a list as list1= [1,2,3,4]. The statement del(list1[2]) will give 1,2, 4.  The element in the second index is 3. That element will be deleted. When there is are two lists as list1= [1,2,3] and list2 = [4,5,6], the programmer can join these two lists using concatenation operation as list1+list2. It will give a combined list [1,2,3,4,5,6].

There is a number of list methods available to handle list operations. Some of them are insert (), remove (), count () etc. Implementing a list in Python is easy when compared to the arrays in other programming languages such as C, C++ etc.

What is Tuple?

A tuple is similar to a list. Each item in the list is separated by a comma. All elements are included in parenthesis. A tuple can have a different type of elements. Each element is separated by a comma. An example of tuple is tuple1 = (1,2,3). The first element has the index 0. The second element has the index 1 and so on.  Tuple can also have negative indexing. So, the value 3 has the index -1. Value 2 gas the index -2 and so on.

The programmer can take a sequence of elements in the tuple. Assume that there is tuple, tuple1= (1,2,3,4,5). The statement print(list1[2:5]) will print 3,4. The element in index two is included but not the element in index five.

Tuples are immutable. Therefore, the elements in the list cannot be changed. Changing the elements will give errors. But if the element is a mutable data type, then its nested items can be changed.  Assume that there is a tuple as tuple1 = (1,2, [3,4]). Even this is a tuple, the element in index 2 has a list. To change the 1st element in that list to 5, the statement tuple1[2][0] = 5 can be used. As the tuple is immutable, the elements cannot be deleted. But using the del function, the entire tuple can be deleted. e.g. del (tuple1).

Figure 01: Examples of a List and a Tuple

There are functions provided by Python for tuple-based operations. The len () function helps to find the number of elements in a tuple.  The max and min functions can be used to find the maximum value and the minimum value of the tuple. Implementing a tuple is an easy process comparing with arrays in another programming language such as C/ C++.

What are the Similarities Between List and Tuple?

  • Both List and Tuple are used to store a set of elements in Python.
  • The index of both list and tuple starts with zero.
  • Each element is separated by a comma in both List and Tuple.
  • Both List and Tuple can have different types of elements.
  • The list can contain nested list and tuple can contain nested tuple.
  • Both List and Tuple support negative indexing.

What is the Difference Between List and Tuple?

List vs Tuple

A list is a compound data type in Python programming language which can store different type of data and can change elements once created. A tuple is a compound data type in Python programming language which can store different type of data and cannot change elements once created.
 Mutability
A list is mutable. It can be changed once created. A tuple is immutable. It cannot be changed once created.
Enclosing Elements
The elements of a list are enclosed in square brackets. The elements of a tuple are enclosed in parenthesis.
 Speed
Iterating through elements in a list is not fast as in a tuple. Iterating through elements in a tuple is faster than list.

Summary – List vs Tuple 

Python uses List and Tuple to store data. The List and tuple can use to store different type of data elements. This article discussed the difference between List and Tuple. The elements in a list can be changed. So, a list is mutable. The elements in a tuple cannot be changed. So, a tuple is immutable. The difference between list and tuple is that a list is mutable while a tuple is immutable.

Download the PDF of List vs Tuple

You can download the PDF version of this article and use it for offline purposes as per citation note. Please download the PDF version here: Difference Between List and Tuple

Reference

1.tutorialspoint.com. “Python Lists.” The Point,  Available here 
2.tutorialspoint.com. “Python Tuples.” The Point. Available here