Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers.
Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. This helps in understanding the Python tuples more easily.
Creating a Tuple
In Python, tuples are created by placing sequence of values separated by ‘comma’ with or without the use of parentheses for grouping of data sequence.
Note – Creation of Python tuple without the use of parentheses is known as Tuple Packing.
Python program to demonstrate addition of elements in a Tuple.
Initial empty Tuple: () Tuple with the use of String: ('Geeks', 'For') Tuple using List: (1, 2, 4, 5, 6) Tuple with the use of function: ('G', 'e', 'e', 'k', 's')
Creating a Tuple with Mixed Datatypes.
Tuples can contain any number of elements and of any datatype (like strings, integers, list, etc.). Tuples can also be created with a single element, but it is a bit tricky. Having one element in the parentheses is not sufficient, there must be a trailing ‘comma’ to make it a tuple.
Tuple with Mixed Datatypes: (5, 'Welcome', 7, 'Geeks') Tuple with nested tuples: ((0, 1, 2, 3), ('python', 'geek')) Tuple with repetition: ('Geeks', 'Geeks', 'Geeks') Tuple with a loop ('Geeks',) (('Geeks',),) ((('Geeks',),),) (((('Geeks',),),),) ((((('Geeks',),),),),)
Accessing of Tuples
Tuples are immutable, and usually, they contain a sequence of heterogeneous elements that are accessed via unpacking or indexing (or even by attribute in the case of named tuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.
NOTE : In unpacking of tuple number of variables on left hand side should be equal to number of values in given tuple a.
First element of Tuple: e Values after unpacking: Geeks For Geeks
Concatenation of Tuples
Concatenation of tuple is the process of joining of two or more Tuples. Concatenation is done by the use of ‘+’ operator. Concatenation of tuples is done always from the end of the original tuple. Other arithmetic operations do not apply on Tuples.
Note- Only same datatypes can be combined with concatenation, an error arises if a list and a tuple are combined.
Tuple 1: (0, 1, 2, 3) Tuple2: ('Geeks', 'For', 'Geeks') Tuples after Concatenaton: (0, 1, 2, 3, 'Geeks', 'For', 'Geeks')
Slicing of Tuple
Slicing of a Tuple is done to fetch a specific range or slice of sub-elements from a Tuple. Slicing can also be done to lists and arrays. Indexing in a list results to fetching a single element whereas Slicing allows to fetch a set of elements.
Note- Negative Increment values can also be used to reverse the sequence of Tuples
Removal of First Element: ('E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S') Tuple after sequence of Element is reversed: ('S', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'G') Printing elements between Range 4-9: ('S', 'F', 'O', 'R', 'G')
Deleting a Tuple
Tuples are immutable and hence they do not allow deletion of a part of it. Entire tuple gets deleted by the use of del() method.
Note- Printing of Tuple after deletion results to an Error.
Traceback (most recent call last):
File “/home/efa50fd0709dec08434191f32275928a.py”, line 7, in
NameError: name ‘Tuple1’ is not defined
|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.|
Recent Articles on Tuple
- Print unique rows in a given boolean Strings
- Program to generate all possible valid IP addresses from given string
- Python Dictionary to find mirror characters in a string
- Generate two output strings depending upon occurrence of character in input string in Python
- Python groupby method to remove all consecutive duplicates
- Convert a list of characters into a string
- Remove empty tuples from a list
- Reversing a Tuple
- Python Set symmetric_difference()
- Convert a list of Tuples into Dictionary
- Sort a tuple by its float element
- Count occurrences of an element in a Tuple
- Count the elements in a list until an element is a Tuple
- Sort Tuples in Increasing Order by any key
- Namedtuple in Python
- Output of Python Programs
- Recent Articles on Python Tuples
- Multiple Choice Questions – Python
- All articles in Python Category
- Python | Remove duplicate tuples from list of tuples
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Combining tuples in list of tuples
- Python | Convert string tuples to list tuples
- Python | How to Concatenate tuples to nested tuples
- Python - Filter all uppercase characters Tuples from given list of tuples
- Python program to find Tuples with positive elements in List of tuples
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Tuples in Python
- Output of python program | Set 12(Lists and Tuples)
- Output of python program | Set 13(Lists and Tuples)
- Output of Python Programs | Set 18 (List and Tuples)
- Output of Python Programs | Set 20 (Tuples)
- Python | Sort Tuples in Increasing Order by any key
- Python | Convert a list of Tuples into Dictionary
- Python | Print unique rows in a given boolean matrix using Set with tuples
- Python | Remove empty tuples from a list