• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

5 Major Difference Between List and Tuple in Python

Aniruddha Chaudhari/74706/12
CodePython

Tuple and List are the very important data structures in Python to store the series of data. In this post, I am demonstrating the difference between list and tuple in Python.

If you are a new Python programmer, let me tell you, it is a collection object like an array in C, C++ programming. An array is far different from list and tuple. You will realize this at the end of this article.

In Python, having two data structures (List and Tuple) with a slight difference is more confusing for the Python programmer. But in context to the use cases of both, they are distinct and both have their advantages and disadvantages.

This is one of the most common Python interview questions asked in many of the interviews.

Let’s start digging without wasting any time…

Table of Contents

  • Difference between List and Tuple in Python:
    • 1. Syntax
    • 2. Mutability Vs Immutability
    • 3. Functionality
    • 4. Python List of Tuples:
    • 5. Where to use Tuple and List?

Difference between List and Tuple in Python:

I am trying to make it simple with examples for you.

1. Syntax

The syntax for the list and tuple are slightly different.

Python List Example:

listWeekDays = ['mon', 'tue', 'wed', 2]

You can check the type of object created using type() function in Python.

type(listWeekDays)
#<class 'list'>

Python Tuple Example:

tupWeekDays = ('mon', 'tue', 'wed', 2)

Using type() function to check the tupWeekDays type.

type(tupWeekDays)
#<class 'tuple'>

list and tuple example in python

If you look into the above code…

Tuple uses ( and ) to bind the elements where a list uses [  and ] to bind the elements in the collection.

You can even create tuple without ( and ) operators. Comma operator (,) is mandatory to distinguish elements in a tuple.

tupWeekDays = 'mon', 'tue', 'wed', 2

When you want to create an empty tuple, you can not create it without ( and ) operators. The only ways are…

tupWeekDays = ()
tupWeekDays = tuple()

It’s very rare you will use an empty tuple as it is immutable and you can not add elements once it is created.
If you don’t know what is an immutable data structure, here you go…

2. Mutability Vs Immutability

A tuple is a list that one cannot edit once it is created in Python code. The tuple is an immutable data structure.

Whereas List is the mutable entity. You can alter list data anytime in your program.

Among all, this is the major difference between these two collections. I was asked in Druva interview questions, why tuple is immutable.

It is easy to demonstrate the mutability difference between tuple and list in Python with an example.

Tuple:

>>> tupWeekDays = ('mon', 'tue', 'wed', 2)
>>> print(tupWeekDays)
('mon', 'tue', 'wed', 2)

>>> tupWeekDays(2)='fri'
Traceback (most recent call last):
File "", line 1, in
TypeError: 'tuple' object does not support item assignment

tuple is immutable in Python

List:

>>> tupWeekDays = ['mon', 'tue', 'wed', 2]
>>> print(listWeekDays)
['mon', 'tue', 'wed', 2]

>>> listWeekDays[2]='fri'
>>> print(listWeekDays)
['mon', 'tue', 'fri', 2]

list is mutable in Python

For more clarification, you must read the difference between mutable and immutable. There, I have explained, why tuple is immutable with more examples in a better way.

3. Functionality

There are more functions are associated with List than Tuple.

You can use a dir([object]) inbuilt function to get all the associated functions for list and tuple.

Simply create a tuple and list. Pass these objects to dir function. You get a list of the associated functions as below.

>>> tupWeekDay=('mon', 'mon', 'wed', 2)

>>> dir(tupWeekDay)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__
format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__get
slice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__
lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__'
, '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count
', 'index']

>>> listWeekDay=['mon', 'mon', 'wed', 2]

>>> dir(listWeekDay)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delsli
ce__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getit
em__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__',
'__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__r
educe__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__'
, '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'a
ppend', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'
]

You can clearly see that, there are so many additional functionalities associated with a list over a tuple. You can do insert and pop operation, removing and sorting elements in the list with inbuilt functions.

Some List Functionality examples you would like to give a try…

  • Randomly Select Item from List in Python
  • Find Unique Elements from List in Python
  • Lambda Function List Comprehension | Map, Filter, Reduce

In tuple, there is no index placed for each element. So you can not do the insert, pop, remove or sort elements. Moreover, it is an immutable data type so there is no way to change the elements in a tuple.

4. Python List of Tuples:

How to create a list as a collection of tuples?

As you know everything in Python is an object (including the tuple and list). The list is a collection of objects.

So you can create a list of tuple objects; code as given below.

listWeekDay=[('mon', 'tue', 'wed'),('sat','sun')]

In the above example, days in the list are separated as tuples, based on weekdays and week off days.

5. Where to use Tuple and List?

If you are writing a program where you need a collection of objects, you have two choices- Tuple vs List.

Sometimes in the program, you may need elements in the collection that can be alerted at any time. You must use a list instead of a tuple.

You can also take keyboard input from the user and insert a new element in a List.

So, what is the advantage of using tuples over lists?

A tuple is immutable in Python and you can not change its elements once it is created. If you are creating collections and elements in the collection are not going to change, you must use Tuple.

In case if you tried to alter the tuple value, it will throw an error. So it prevents the value from changing.

Note: All the above-demonstrated examples are executed and tested on Python 2.7 and it will run on Python 3 as well. For every example, I have a clear python interpreter console and executed to avoid the mess.

These are all the difference between list and tuple in Python. If you have any thoughts on these, write in a comment.

Python Interview Questions eBook

python listpython tuple
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Amrender Pal
    May 30, 2017 at 7:28 pm

    It is really an awesome post. I work as Python developer. You have cleared many of the doubts about list and tuple.

    Keep it up your good work.

    One more thing… Please update me if you post any Python article in future.

    • Reply
      Aniruddha Chaudhari
      May 31, 2017 at 3:51 am

      Thanks, Amrender! I am glad as you find this article useful to improve your Python skillset.

      For updates, you can subscribe to our weekly newsletter.

      Stay tuned!

  • Reply
    J. Zack
    June 1, 2017 at 2:15 am

    Under 1. Syntax, Line 1 should read:
    listWeekDays = [‘mon’, ‘tue’, ‘wed’, 2]

    • Reply
      Aniruddha Chaudhari
      June 1, 2017 at 3:40 am

      Hi Zack,
      It was a typo mistake.
      Updated. Thanks for correcting me!

  • Reply
    Shashank
    March 9, 2019 at 12:53 am

    An element can be added to an existing tuple, is this an exception to the immutability property of the tuple?

    • Reply
      Aniruddha Chaudhari
      March 9, 2019 at 9:07 am

      You can not modify the elements in the tuple. When you do the workaround to add a new element in the tuple, it might be creating a new tuple.

      You can have look here – https://www.csestack.org/difference-between-mutable-and-immutable-in-python/#tuple to understand the immutable nature of tuple.

  • Reply
    udaysree Buchupalli
    March 25, 2019 at 5:35 am

    Hi, I am using Python 3.7 version. I have tried changing the list using list WeekDays(2)=’fri’. But, I am getting the error
    can’t assign to function call.

    • Reply
      Aniruddha Chaudhari
      March 25, 2019 at 9:20 am

      Hi Udaysree, there was a mistake in our code. I have corrected it in the tutorial. To change the element in the list, you have to use a square bracket. listWeekDays[2]=’fri’ is the right way of doing this.

      Thanks for the correction.

  • Reply
    Nagababu
    June 27, 2019 at 3:49 pm

    Well, why the tuples are immutable? Are there any applications of it?

    • Reply
      Aniruddha Chaudhari
      June 27, 2019 at 8:32 pm

      Both data types serve a different purpose. We already have discussed this. Kindly check- Why is Tuple Immutable Data Type in Python?

  • Reply
    SoheebAziz
    March 2, 2020 at 5:44 pm

    Typo for “A tuple is immutable in Python and you can no change…” toward the end (5th last sentence). It should read “you cannot change…”

    • Reply
      Aniruddha Chaudhari
      March 2, 2020 at 6:53 pm

      Thanks for the correction, SoheebAziz! Edited.

Leave a Reply Cancel reply

Basic Python Tutorial

  1. Python- Tutorial Overview
  2. Python- Applications
  3. Python- Setup on Linux
  4. Python- Setup on Windows
  5. Python- Basic Syntax
  6. Python- Variable Declaration
  7. Python- Numeric Data Types
  8. Python- NoneType
  9. Python- if-else/elif
  10. Python- for/while else
  11. Python- User Input
  12. Python- Multiline User Input
  13. Python- String Formatting
  14. Python- Find Substring in String
  15. Python- Bitwise Operators
  16. Python- Range Function
  17. Python- List
  18. Python- List Vs Tuple
  19. Python- Compare Two Lists
  20. Python- Sorting List
  21. Python- Delete Element from List
  22. Python- Dictionary
  23. Python- ‘is’ vs ‘==’
  24. Python- Mutable vs Immutable
  25. Python- Generator & Yield
  26. Python- Fibonacci Generator
  27. Python- Assert Statement
  28. Python- Exception Handling 
  29. Python- RegEx
  30. Python- Lambda Function
  31. Python- Installing Modules
  32. Python- Important Modules
  33. Python- Find all Installed Modules
  34. PyCharm- IDE setup
  35. Python- File Handling
  36. Python- Monkey Patching
  37. Python- Decorators
  38. Python- Instance vs Static vs Class Method
  39. Python- Name Mangling
  40. Python- Working with GUI
  41. Python- Read Data from Web URL
  42. Python- Memory Management
  43. Python- Virtual Environment
  44. Python- Calling C Function

Python Exercise

  1. Python- Tricky Questions
  2. Python- Interview Questions (60+)
  3. Python- Project Ideas (45+)
  4. Python- MCQ Test Online
  5. Python- Coding Questions (50+)
  6. Python- Competitive Coding Questions (20+)

Python String

  1. Reverse the String
  2. Permutations of String
  3. Padding Zeros to String/Number

Python List

  1. Randomly Select Item from List
  2. Find Unique Elements from List
  3. Are all Elements in List Same?

Python Dictionary

  1. Set Default Value in Dictionary
  2. Remove all 0 from a dictionary

File Handling

  1. Python- Read CSV File into List
  2. Check if the File Exist in Python
  3. Find Longest Line from File

Compilation & Byte Code

  1. Multiple Py Versions on System
  2. Convert .py file .pyc file
  3. Disassemble Python Bytecode

Algorithms

  1. Sorting- Selection Sort
  2. Sorting- Quick Sort

Other Python Articles

  1. Clear Py Interpreter Console
  2. Can I build Mobile App in Python?
  3. Extract all the Emails from File
  4. Python Shell Scripting

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard