×

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

Dynamic Typing in Python

Many key factors for developing a great programming language include how it manages its memory space. This memory space largely depends on empty memory boxes called variables that one creates...

3 minutes read.

Python Modules List

Python is like an ocean. If you have been working with Python, you might’ve already heard the word module. When we solve a problem in mathematics, there will be several...

3 minutes read.

Python NewLine

In python, a new line character denoted by "\n" is known as an escape character or escape sequence, and it will force the cursor to change its position to the next...

6 minutes read.

Python sum() function

Python sum() function The sum() function in python sums start and the items of an iterable from left to right and returns the total.  Syntax sum(iterable[, start]) Parameter iterable: This parameter represents the sequence to sum. start: It is optional...

1 minute read.

Time. Sleep() in Python

Python time sleep () function suspends execution for a certain seconds given by user. Time sleep () syntax: Sleep(seconds) Limitations: The number of seconds to be suspends the code as per its requirements. Returns: Void The execution...

3 minutes read.

An Introduction to Subprocess in Python with Examples

Subprocess in Python By starting new processes, the Python function subprocess is utilized to run extra programs and applications. It makes it possible to run brand-new apps straight from a Programming...

6 minutes read.

Python Random Module

The tutorial for the Python random module demonstrates how to produce pseudo-random integers in Python. Random Number Generator (RNG) The RNG (random number generator) generates a series of values with no discernible...

8 minutes read.

Python filter() function

Python filter() function The filter() function constructs an iterator from those elements of iterable for which the parameter ‘function’ returns a Boolean value true. Syntax filter(function, iterable) Parameter function: This parameter represents a function to be run for each item in the...

1 minute read.

Number pattern in Python

Number pattern in Python: This article explains how to print number patterns in Python. The FOR loop, while loop, and range() functions are used in the following Python programs to...

4 minutes read.

FOO in 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...

3 minutes read.

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

How to Install Scikit-Learn

Sklearn or Scikit-learn is a python library used for machine learning. It contains many features like classification, regression, clustering, and Dimensionality reduction algorithms. Sklearn is used to build machine learning...

3 minutes read.

Python random.seed() function

The random module in Python produces a random number or pseudo-random data, that is, deterministic. The seed function records the state of a random function to provide the same random...

6 minutes read.

Python IDE

What is an IDE?  An IDE or an Integrated Development Environment is a type of software where developers can develop many software’s and applications and run the programs inside it. An...

11 minutes read.

How to assign values to variables in Python and other languages?

Python makes it simple to construct variables. The value to be stored in the variable should then be written after a suitable name for the variable and the equality sign....

3 minutes read.

Inheritance in Python

Inheritance in Python Object-Oriented Programming provides reusable patterns to the code for restricting the redundancy in development projects. One of the basic principles of Object-Oriented Programming that helps achieve recyclable code...

8 minutes read.

Python Empty Tuple

How to Create an Empty Tuple Tuple A tuple is a data structure used to store non-homogeneous data elements. These non-homogeneous data elements consist of integer data type, character data type, String...

3 minutes read.

IPython Display

A command shell, often known as IPython is a software application that makes an operating system's features accessible to users or other applications. Based on the purpose and specific functioning...

6 minutes read.

Pointers in Python

In this tutorial, we will study what pointers are and if they have any utility in python. Now, let us understand what pointers are Pointers Pointers are special variables used to store the...

3 minutes read.

Python Dictionary Methods

Python Dictionary Methods Python has a set of built-in methods that dictionary objects can call. All the python dictionary methods are as follow: Methods Description clear() The dictionary.clear() method removes all the elements...

3 minutes read.