×

Difference between python list and tuple

In this tutorial, we will understand the key differences between python lists and tuples in python.

Firstly, let us see the similarities between Lists and Tuples. Both are two data types popular in python. They are used to accumulate one or more Python objects or data types serially. Both are capable of storing data such as integers, floats, strings, and dictionaries.

Now, let us understand the difference between both.

1. Difference in Representation

The Lists and tuples are represented differently.

The square bracket [] is taken into account to enclose a list, and elements therein are separated by a comma.

The parenthesis () is taken into account to enclose a tuple and elements therein are separated by the comma.

Let us see an example to understand this better.

Example:

  # online python-3 complier (Interpreter)
  # python program to mark the representational difference between list and tuple
  list_1=[‘tutorials and example’, 15, 92, 984.70, {‘Name: “Python”}]
  print(type(list_1))
  tuple_1=(‘Tutorials and example’ ,95, 78, 31.9, [16, 82,n93])
print(type(tuple_1))

Output:

Difference between python list and tuple

Explanation: In the above code, the representational difference between the data types – list and tuple are being addressed.

We can see that list named list_1 is represented using a square bracket and the tuple named tuple_1 is represented using parenthesis.

2. Mutability and Immutability in python

It is the most significant variance between list and tuple. The lists are mutable, and tuples are immutable. By the mutability of the list, we mean the objects in python can be modified after creation, whereas tuples once created can't be modified.

Example to show the mutable nature of the list.

# Python program to illustrate the mutable nature of lists in python


list1 = ["Anthropology", "Geography", "Hindi", "Polity", "History"]  
print("list before modification: ")
print(list1)  
list1[0] = "English"  
print("list after modification: ")
print(list1)   

Output:

Difference between python list and tuple

Explanation: The above code shows the mutable nature of the list. The first line of code shows the unmodified list. In the second line, we are trying to modify the 0th index of the list. We are able to successfully modify the list. The previous element ‘anthropology’ is being replaced with the word English. the modified list is shown. Here, we can modify the list.

Example to show the immutable nature of the tuple.

#Python program to illustrate the immutable nature of tuple in python


tuple1 = ("Anthropology", "Geography", "Hindi", "Polity", "History") 
print("tuple before modification: ")
print(tuple1)  


tuple1[0] = "English"  
print("tuple after modification: ")
print(tuple1)  

Output:

Difference between python list and tuple

Explanation: The above code shows the immutable nature of the tuple. The first line of code shows the unmodified tuple. In the second line, we are trying to modify the 0th index of the list but failed. We are unable to modify the tuple. Hence, it proves that the tuple cannot be modified.

3. Debugging

It is easier to debug the tuples when working on a larger project as they are immutable in nature. If the size of the project is small or the number of data is less, then lists play a vital role. Now, let us understand this through an example.

#python program to illustrate the debugging in a list
lt1 = [236, 95,443, 83, 17, 890, 3211]  
# Copying address of a in b  
lt2 = lt1  
lt1[6] = "Tutorial and example"  
print(lt1)  

Output:

Difference between python list and tuple

Explanation: In the above code, we tried to do lt2 = lt1; here we are not copying the list object from blt2 to lt1. The lt2 referred to the address of the list lt1.  It means if we make the changes in the lt2 then it will also get reflected in list lt1, and it makes debugging easy. But it becomes tough for the substantial project where Python objects may have several references.

It will thus become very complicated to track those fluctuations in lists but it is not possible to change the immutable object tuples after it gets created.

Thus, it is easy to debug a tuple.

4. Supported functions

Fewer operations are supported in tuples than in the list. The inbuilt function named dir(object) is used to fetch all the supported functions for the lists and tuples.

List Functions

dir(list)  


Output:

Difference between python list and tuple

Tuple Functions

dir(tuple)  

Output:

Difference between python list and tuple
Difference between python list and tuple

5. Efficiency in memory

The lists are less efficient in terms of memory than the tuple as the tuple has fewer built-in operations. Lists are appropriate for the smaller number of elements whereas tuples are a bit faster than the list for the enormous amount of data.

Example:

#python program to analyze the memory efficiency


Tuple = (145, 42, 32, 894, 65, 126, 897, 138, 909, 30, 7805, 1222, 815, 831, 8234,9208, 234, 56, 234, 535)  


List = [11, 325, 245, 903, 14, 75, 76, 17, 88, 19, 40, 178, 84, 743, 832, 343, 555, 74, 941, 302, 723, 784, 74, 532 ]  


print('The size of tuple =', Tuple.__sizeof__())       
            print('The size of list =', List.__sizeof__())   

Output:

Difference between python list and tuple

Difference Between Python List and Tuple

Difference between python list and tuple
ParameterListTuple
MutabilityMutableImmutable
speedSlowFast
Area of performanceInsertion and deletionAccessing elements
MemoryConsumes more memoryConsumes less memory
Stored elementCapable of storing homogeneous as well as heterogeneous elements.Capable of storing only heterogeneous elements.
Built-in methodsMany built-in methodsLesser in-built methods
RepresentationSquare braces []Parenthesis ()

Related Topics

Python MySQL

In this article, we are going to learn the following: How to connect Python to MySQL.How to create a new Database.Procedure for connecting the newly created database.Procedure for connecting the already...

7 minutes read.

Python ascii() Function

Python ascii() Function The ascii() function returns a readable version of any object (Strings, Tuples, Lists, etc). This function will replace any non-ascii characters with escape characters. Syntax ascii(object) Parameter object:  An object, like String, List, Tuple, Dictionary, etc. Return This...

1 minute read.

Python Linear regression

In this tutorial, we will understand the meaning, usage, and types of Linear Regression. Further, we will comprehend the terms cost function and Optimization. Linear Regression is the widely used Machine...

3 minutes read.

Uint8 Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

XXhash Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

6 minutes read.

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

Python String isalnum() method

Python String isalnum() method The string.isalnum () method in Python returns a boolean value true if all characters in the string are alphanumeric else for any other value it returns false. Syntax String.isalnum() Parameter NA Return This...

1 minute read.

Python property()

Python property() class The property() class in Python returns a property attribute. Syntax class property(fget=None, fset=None, fdel=None, doc=None) Parameter fget: This attribute is used for getting an attribute value. fset : This parameter sets an attribute value. fdel: It is a function for deleting...

1 minute read.

Sys Module in Python

What are Modules? The Modules are the kind of files that contain Python statements and definitions. The module is known by the name of the file followed by the suffix “.py”....

5 minutes read.

Confusion Matrix Visualization Python

The confusion matrix is a two-dimensional array that compares the anticipated and actual category labels. These are the True Positive, True Negative, False Positive, and False Negative classification categories for...

4 minutes read.

How to Program in Python on Raspberry pi?

Introduction to Python A popular programming tool with simple, complete novice syntax is Python structure of paragraphs, phrases, and words. Due to its widespread use, this has a large community that...

4 minutes read.

Python maketrans() function

Introduction A static method is the string maketrans() one. It is used to make a one-to-one mapping between a string's character and its translation, i.e., to define the list of characters...

5 minutes read.

Genetic Algorithm in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The source...

4 minutes read.

Python String find() method

Python String find() method The string.find() method in Python finds the first occurrence of the specified value and returns the lowest index in the string if the substring sub is found else it...

2 minutes read.

Palindrome program in Python

Palindrome program in python A number or string is said to be a palindrome if we invert the number or string and the string or number remains the same as the...

3 minutes read.

Working with CSV files in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

7 minutes read.

Python input() function

Python input() function The input() function in Python reads a line from input and converts it to a string (stripping a trailing newline). Syntax input([prompt]) Parameter prompt: This function represents a string, depicting a default...

1 minute read.

Mrcnn Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

GUI Calendar using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Python String endswith() method

Python String endswith() method The string.endswith() method in Python returns a Boolean value True if the string ends with the specified suffix, otherwise it returns False. Syntax endswith(suffix[, start[, end]]) Parameter suffix – This parameter represents a string or tuple...

2 minutes read.