A tuple is similar to a list. The difference between the two is that we cannot change the elements of a tuple once it is created whereas in a list, elements can be changed.
Tuple | List |
---|---|
immutable -Once created contents cannot be changed | mutable -contents can be changed |
Generally use tuple for heterogeneous (different) datatypes | list for homogeneous (similar) datatypes. |
Example: date_of_birth=(10,"aug",2000) | Example: heights=[150,160,170] |
Iteration is slightly faster than list | relatively slower than tuple |
>>> tu=(1,2,3,4)
>>> tu.append(5) # Once created they cannot be changed
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'
>>>
Elements can be accessed with index
Negative Index is also supported
We can access a range of items in a tuple by using the slicing operator - colon ":". General syntax
>>> tu=(1,2,3,4) # Create a tuple
>>> tu[:] # Get all elements in the tuple, Note start and end are optional
(1, 2, 3, 4)
>>> tu[:-1] # Get all the elements from start
(1, 2, 3)
>>> tu[0:] #Get all the elements till end
(1, 2, 3, 4)
>>> tu[0:1] # Get first element in the tuple
(1,)
>>> tu[0:-1:2] # You can specify step size as well
(1, 3)