Difference between tuple and list in Python
In this article, you will learn the difference between tuple and list in Python. The difference between tuple and list is that tuple stores heterogeneous data while the list stores homogeneous data.
Python List
A list is a sequence of indexed and ordered values like an array. It is mutable, means we can change the order of elements in a list. So, we should use list if the contents of the sequence may change. It contains list of any type of data objects with comma separated and enclosed within an square bracket. Each element of list has an index, starting with 0 , 1 and so on. Here is the format of a list -
[expr1, expr2, ...]
The zero or more values of the list are enclosed within square brackets [...].
Example of Python list
>>> list = [23, 10, "Test", 13]
>>> print list
[23, 10, 'Test', 13]
Python Tuple
A tuple is a sequence. Python Tuples work exactly like a list except tuples are immutable, means it cannot be changed in place. The tuples are written inside parentheses. The tuple length is always fixed. To create a single element in a tuple, write that element in parenthesis with a comma.
tu1 = (60,)
tup1 = (22, 44, 55, 12 )
Difference Between Tuple and List in Python
List | Tuple |
List is mutable. It can be modified after creation. | Tuple is immutable. It can't be modified after creation. |
Iteration is slower and time consuming. | Iteration is comparatively faster and less time consuming. The processing is faster than list. |
It consumes more memory. | It consumes less memory. |
It is useful for performing operations, like - addition, deletion. | This data type is suitable for accessing elements. |
It has many in-built methods. | It provides less in-built methods. |
Lists play an effective role in small projects. | Tuple is easy to debug even in major projects. |
Lists are commonly enclosed with the square bracket []. | Tuples are enclosed with parenthesis (). |
Related Articles
Vader Sentiment Analysis Python
isalpha Python
Python YouTube Downloader Script
Python project ideas for beginners
Pandas string to datetime
Fillna Pandas Example
Lemmatization nltk
How to generate QR Code in Python using PyQRCode
OpenCV and OCR Python
PHP code to send SMS to mobile from website
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml