![]() |
VOOZH | about |
Python Tuple is a collection of objects separated by commas. A tuple is similar to a Python list in terms of indexing, nested objects, and repetition but the main difference between both is Python tuple is immutable, unlike the Python list which is mutable.
(10, 20, 30) <class 'tuple'>
Unlike Python lists, tuples are immutable. Some Characteristics of Tuples in Python.
Let us see this with an example.
Output:
2
5
(1, 2, 3, 4, 2, 3)
Traceback (most recent call last):
File "Solution.py", line 12, in <module>
t[1] = 100
TypeError: 'tuple' object does not support item assignment
Tuples in Python provide two ways by which we can access the elements of a tuple.
Using square brackets we can get the values from tuples in Python.
Value in tup[0] = 10 Value in tup[1] = 5 Value in tup[2] = 20
In the above methods, we use the positive index to access the value in Python, and here we will use the negative index within [].
Value in tup[-1] = 20 Value in tup[-2] = 5 Value in tup[-3] = 10
Below are the different operations related to tuples in Python:
Like List Traversal, we can traverse through a tuple using for loop.
1 2 3 4 5
To Concatenation of Python Tuples, we will use plus operators(+).
(0, 1, 2, 3, 'python', 'geek')
A nested tuple in Python means a tuple inside another tuple.
((0, 1, 2, 3), ('python', 'geek'))
We can create a tuple of multiple same elements from a single element in that tuple.
('python', 'python', 'python')
Try the above without a comma and check. You will get tuple3 as a string 'pythonpythonpython'.
Slicing a Python tuple means dividing a tuple into small tuples using the indexing method. In this example, we slice the tuple from index 1 to the last element. In the second print statement, we printed the tuple using reverse indexing. And in the third print statement, we printed the elements from index 2 to 4.
(1, 2, 3) (3, 2, 1, 0) (2, 3)
Note: In Python slicing, the end index provided is not included.
In this example, we are deleting a tuple using 'del' keyword. The output will be in the form of error because after deleting the tuple, it will give a NameError.
Note: Remove individual tuple elements is not possible, but we can delete the whole Tuple using Del keyword.
Output:
Hangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 5, in <module>
print(t)
NameError: name 't' is not defined
To find the length of a tuple, we can use Python's len() function and pass the tuple as the parameter.
2
Tuples in Python are heterogeneous in nature. This means tuples support elements with multiple datatypes.
('immutable', True, 23)
We can convert a list in Python to a tuple by using the tuple() constructor and passing the list as its parameters.
(0, 1, 2)
Output:
Tuples take a single parameter which may be a list, string, set, or even a dictionary(only keys are taken as elements), and converts them to a tuple.
We can also create a tuple with a single element in it using loops.
(('gfg',),)
((('gfg',),),)
(((('gfg',),),),)
((((('gfg',),),),),)
(((((('gfg',),),),),),)
('gfg', 'Python')
(4, 5, 6)
(7, 8, 9)
()
(10,) <class 'tuple'> 10 <class 'int'>
(11, 12, 13)
Tuples support only a few methods due to their immutable nature. The two most commonly used methods are count() and index()
| Built-in-Method | Description |
|---|---|
| index( ) | Find in the tuple and returns the index of the given value where it's available |
| count( ) | Returns the frequency of occurrence of a specified value |
| Built-in Function | Description |
|---|---|
| all() | Returns true if all element are true or if tuple is empty |
| any() | return true if any element of the tuple is true. if tuple is empty, return false |
| len() | Returns length of the tuple or size of the tuple |
| enumerate() | Returns enumerate object of tuple |
| max() | return maximum element of given tuple |
| min() | return minimum element of given tuple |
| sum() | Sums up the numbers in the tuple |
| sorted() | input elements in the tuple and return a new sorted list |
| tuple() | Convert an iterable to a tuple. |
| Similarities | Differences |
|---|---|
Functions that can be used for both lists and tuples: len(), max(), min(), sum(), any(), all(), sorted() | Methods that cannot be used for tuples: append(), insert(), remove(), pop(), clear(), sort(), reverse() |
Methods that can be used for both lists and tuples: count(), Index() | we generally use 'tuples' for heterogeneous (different) data types and 'lists' for homogeneous (similar) data types. |
| Tuples can be stored in lists. | Iterating through a 'tuple' is faster than in a 'list'. |
| Lists can be stored in tuples. | 'Lists' are mutable whereas 'tuples' are immutable. |
| Both 'tuples' and 'lists' can be nested. | Tuples that contain immutable elements can be used as a key for a dictionary. |