Differences between List and Tuple in Python

List and Tuple are exactly same except small differences such as List objects are mutable whereas Tuple objects are immutable. Mutable means changeable while immutable means unchangeable.  In both cases insertion order is preserved, duplicate objects are allowed, heterogenous objects are allowed, index and slicing are supported.

List vs Tuple

Here are the basic differences between list and tuple data structure in Python.

                                        List                                Tuple
1. List is a Group of Comma separated Values within Square Brackets and Square Brackets are mandatory.
Eg: l = [10, 20, 30, 40]
1. Tuple is a Group of Comma separated values within Parenthesis and Parenthesis are optional.
Eg: t = (10, 20, 30, 40)
t = 10, 20, 30, 40
2. List Objects are Mutable i.e. once we creates List Object we can perform any changes in that Object.
Eg: l[1] = 70
2. Tuple Objects are Immutable i.e. once we creates Tuple Object we cannot change its content.
t[1] = 70 ==> ValueError: tuple object does not support item assignment.
3. If the Content is not fixed and keep on changing then we should go for List. 3. If the content is fixed and never changes then we should go for Tuple.
4. List Objects can not be used as keys for dictionaries because keys should be Hashable and Immutable. 4. Tuple Objects can be used as Keys for Dictionaries because Keys should be Hashable and Immutable.

Conclusion

In this guide, you learnt the differences between list and tuple in Python. We have various tutorials on Python here, you can go through them. Please stay tuned for more. 

 

Read Also : Python Lists

Read Also :  Python Tuple

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

1 thought on “Differences between List and Tuple in Python”

  1. Pingback: Python Sets

Leave a Comment