EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Python Tuple vs List

Home » Software Development » Software Development Tutorials » Top Differences Tutorial » Python Tuple vs List

Python Tuple vs List

Differences Between Python Tuple and List

Python Tuple is used for defining and storing a set of values by using (), which is the curly parenthesis. On the other hand, List is used for defining and storing a set of values by using the square brackets represented as []. When comparing the built-in functions for Python Tuple and the list, Python Tuple has lesser pre-defined built-in functions than the lists. A few of the advantages of lists against the Python Tuple are that the list can be defined for variable lengths and can be copied from one destination to another, but Python Tuple can have only fixed lengths and cannot be copied.

Let us look at this example for a tuple:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

def test()
return 1,2
a,b = test()
print(a,b)

Here, we have used a tuple to return the value from the function, and hence while printing the value, we cannot modify it.

A Python Tuple can either have no brackets around it or parenthesis like “()” This is what helps Python understand a list from a tuple. Python list is defined by square brackets. Its functionality is similar to how an array works in other languages. Example:

x = [1,3,5,6,2,1,6] print(x)                 : Prints the complete list
print(x[0],x[1])   : Prints the list starting at Index 0

Often confused due to their resemblances, these two structures are significantly different.

Head to Head Comparison Between Python Tuple and List (Infographics)

Below is the Top 6 Comparison Python Tuple vs List

Python Tuple vs List infographics

Key Differences Between Python Tuple and List

Below are the lists of points, describe the key differences between Python Tuple vs List:

Popular Course in this category
Python Training Program (36 Courses, 13+ Projects)36 Online Courses | 13 Hands-on Projects | 189+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.8 (8,365 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)HTML Training (12 Courses, 19+ Projects, 4 Quizzes)

Functionality of Operations

Lists have supplementary builtin function when compared to tuples. Lists and tuples have in common the index() and count() methods, but other than these, lists have many functions that apply only specifically to lists like append(), remove(), clear(), sort(), reverse(), etc.

We can use the inbuilt function dir([object]) to find out all the functions associated with lists and tuples. The output for such a command would be as follows:

List Object:

[‘__add__’,
‘__class__’,
‘__contains__’,
‘__delattr__’,
‘__delitem__’,
‘__dir__’,
‘__doc__’,
‘__eq__’,
‘__format__’,
‘__ge__’,
‘__getattribute__’,
‘__getitem__’,
‘__gt__’,
‘__hash__’,
‘__iadd__’,
‘__imul__’,
‘__init__’,
‘__init_subclass__’,
‘__iter__’,
‘__le__’,
‘__len__’,
‘__lt__’,
‘__mul__’,
‘__ne__’,
‘__new__’,
‘__reduce__’,
‘__reduce_ex__’,
‘__repr__’,
‘__reversed__’,
‘__rmul__’,
‘__setattr__’,
‘__setitem__’,
‘__sizeof__’,
‘__str__’,
‘__subclasshook__’,
‘append’,
‘clear’,
‘copy’,
‘count’,
‘extend’,
‘index’,
‘insert’,
‘pop’,
‘remove’,
‘reverse’,
‘sort’]

Tuple Object:

[‘__add__’,
‘__class__’,
‘__contains__’,
‘__delattr__’,
‘__dir__’,
‘__doc__’,
‘__eq__’,
‘__format__’,
‘__ge__’,
‘__getattribute__’,
‘__getitem__’,
‘__getnewargs__’,
‘__gt__’,
‘__hash__’,
‘__init__’,
‘__init_subclass__’,
‘__iter__’,
‘__le__’,
‘__len__’,
‘__lt__’,
‘__mul__’,
‘__ne__’,
‘__new__’,
‘__reduce__’,
‘__reduce_ex__’,
‘__repr__’,
‘__rmul__’,
‘__setattr__’,
‘__sizeof__’,
‘__str__’,
‘__subclasshook__’,
‘count’,
‘index’]

We can see that there are additional functionalities linked with a list than for a tuple.

Size Evaluation

Tuple operations have a smaller size than that of list operations. This makes the operations faster when there is an enormous number of elements. Let us see an example to calculate the size of the list and tuple elements.

x= (1,2,3,4,5,6,7,8,9,0)
y= [1,2,3,4,5,6,7,8,9,0]  
print(‘x=’,x.__sizeof__())
print(‘y=’,y.__sizeof__())

Output:

x= 104
y= 120

In this example, we have a tuple x and a list y holding the same number of items, but the size of the tuple is less than that of the list.

Diverse Use Cases

Initially, it may seem like lists will always be able to replace tuples, but this is not the case. We can understand this due to the following reasons:

  • When a tuple is being used as opposed to a list, it gives the viewer an idea that the data that is present can and should not be changed.
  • Tuples are frequently used to store data as the equal of a dictionary without keys.

Example:

[(‘Employee1’, 1000), (‘Employee2’, 1001), (‘Employee3’, 1002)]

Tuples can also be used as keys in dictionaries because of their hashtable and immutable nature. Lists are not appropriate for this as they cannot handle the function __hash__() and are mutable in nature.

key_value = {(‘a’,’b’):1}      #Valid
key_value = {[‘a’,’b’]:1}      #Invalid

  • Readability is increased, i.e. reading information is easier when tuples are stored in a list, as opposed to when lists are stored in a list. Example:
[(2,4), (5,7), (3,8), (5,9)] is easier to read than [[2,4], [5,7], [3,8], [5,9]]

Usage of Tuples and Lists

We use a tuple when we know what information is to be given and we need to save the values from being modified, like when we need to store credentials for a website. Tuples are also used as keys for a dictionary because only immutable values can be hashed. Hence, we cannot use a list in such cases. If we still want to use a list as a key, we need to first convert the list into a tuple.

And the other hand, we can use a list, when we want to modify the values given within the collection, and also when we do not know whether our collection size is fixed.

Python Tuples vs Lists Comparison table

Below is the topmost comparison between Python tuples vs Lists

Features Lists

Tuples

Syntax Lists are defined by square brackets []. Tuples are defined by parenthesis () or no brackets at all.
Mutable vs Immutable The list is mutable in nature i.e. it can be altered or changed after its creation. A tuple is immutable in nature i.e. it cannot be altered or changed after its creation.

 

Available Operations Built-in functions in list are more compared to those in tuple, e.g. pop(), insert(), etc. Tuple has fewer built-in functions.
Size Comparison List operations are bigger in size when compared to tuple operations. Tuple operations are smaller in size, which makes it faster with many elements.

 

Length Lists have a variable length.

 

Tuples have fixed lengths. We cannot change the size of an existing tuple.
Duplicity Lists can be copied Tuples cannot be copied.

Conclusion

This is all about Python Tuples vs Lists. So now that we have understood the differences between python tuples and lists, it will be easier for us to decide which of the two should be used and where. Therefore, we can conclude that although both lists and tuples are important data structures in Python, there are notable differences among them, with the major difference being that lists are mutable whereas tuples are not.

Recommended Article

This has been a guide to the Difference between python Tuple vs List. Here we also discuss the key differences with infographics and comparison table. You may also look at the following articles to learn more-

  1. Smoke Testing vs Sanity Testing
  2. Python Compilers
  3. Python List Comprehension
  4. Python getattr

Python Training Program (36 Courses, 13+ Projects)

36 Online Courses

13 Hands-on Projects

189+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
NumPy savetxt

NumPy genfromtxt

Vue.js Form

NumPy divide

NumPy Broadcasting

Pandas vs NumPy

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you