Differences between tuples and lists in python
Python in-build four data types provide. the differences between tuples and lists. the tuples are not unchangeable lists, and lists are changeable data. collection of the data types are following list, tuple, set, and dictionary.
list
Python list() is a collection of data that is ordered and changeable. python lists data are written in array brackets []
.
Example :
list_data = ["one", "two", "three"] print(list_data) output : ['one', 'two', 'three'] # Modify List list_data[1] = "four" print(list_data) output : ['one', 'four', 'three']
Tuple
Python tuple is a collection of data that is ordered and unchangeable. python tuples data are written in round brackets ()
.
Example :
tuple_data = ("one", "two", "three") print(tuple_data) output : ('one', 'two', 'three') # Modify Tuple tuple_data[2] = "four" print(tuple_data) output : Traceback (most recent call last): File "test.py", line 7, in tuple_data[2] = "four" TypeError: 'tuple' object does not support item assignment
list | tuple |
list() is a collection of data that is ordered and changeable. | A tuple() is a collection of data that is ordered and unchangeable. |
Python lists data are written in array brackets ex: [] | Python tuples data are written in round brackets ex: () |