In this post we will be discussing Differences between List and Tuple in python.
TUPLE Data Structure in PYTHON
List and Tuple are exactly same except small difference: List objects are mutable where as Tuple objects are immutable.
In both cases insertion order is preserved, duplicate objects are allowed, heterogenous objects are allowed, index and slicing are supported.
List | Tuple |
1) List is a Group of Comma separated Values within Square Brackets and Square Brackets are mandatory. Eg: i = [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: i[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 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.
|
Related Posts:
Python Tutorial – Learn Python
What is Python? What makes Python so Powerful?
Variables in Python – Constant, Global & Static Variables
Namespacing and Scopes in Python
TUPLE Data Structure in PYTHON
DICTIONARY Data Structure in PYTHON
Database Programming in Python
What is Multithreading in Python?
Python Exception Handling Using try, except and finally statement