What is a tuple in Python? A tuple in Python is an immutable sequence of values, similar to a list. However, tuples cannot be modified once they are created, which means you cannot add, remove or change elements in a tuple.
Creating Tuple in Python
To create a tuple, you can use parentheses
() and separate the elements with commas
Here's an example of how to create a tuple in Python with three elements:
my_tuple = (1, "hello", 3.14)
You can also create an empty tuple by using empty parentheses ():
empty_tuple = ()
If you want to create a tuple with only one element, you need to add a comma after the element, because without it, Python will interpret the parentheses as just grouping operators, not as a tuple:
single_tuple = (1,)
You can access elements of tuple in Python using indexing, just like in lists.
As we already mentioned, the indexing in Python starts from 0. It means that the first element in a tuple has an index of 0, the second element has an index of 1, and so on. Negative indexing is also supported, which means the last element in a tuple has an index of -1, the second to last element has an index of -2, and so on.
Here is an example of accessing tuple elements using indexing:
my_tuple = ('apple', 'banana', 'cherry') print(my_tuple) # Output: 'apple' print(my_tuple) # Output: 'banana' print(my_tuple[-1]) # Output: 'cherry'
Tuple Addition in Python
It is not possible to add elements to an existing tuple directly.
However, you can create a new tuple by concatenating or adding two or more tuples using the
+ operator. Here's an example how to add one tuple to another:
tuple1 = (1, 2, 3) tuple2 = (4, 5, 6) tuple3 = tuple1 + tuple2 print(tuple3) # Output: (1, 2, 3, 4, 5, 6)
You can also use the
* operator to create a new tuple that contains multiple copies of an existing tuple. Here's an example:
tuple1 = (1, 2, 3) tuple2 = tuple1 * 2 print(tuple2) # Output: (1, 2, 3, 1, 2, 3)
Sorted List of Tuples
You can sort a list of tuples in Python using the sorted function, and passing a key argument that specifies how to compare the elements in each tuple. Here's an example:
# define a list of tuples my_list = [(1, 2), (3, 1), (2, 4)] # sort the list by the first element in each tuple sorted_list = sorted(my_list, key=lambda x: x) print(sorted_list) # Output: [(1, 2), (2, 4), (3, 1)]
You can also sort the list of tuples in reverse order by setting the reverse argument to True:
# define a list of tuples my_list = [(1, 2), (3, 1), (2, 4)] # sort the list by the second element in each tuple in reverse order sorted_list = sorted(my_list, key=lambda x: x, reverse=True) print(sorted_list) # Output: [(2, 4), (1, 2), (3, 1)]
Named Tuple in Python
A named tuple is a subclass of the built-in tuple data type that allows for fields to be accessed by name as well as by index position.
Named tuples are created using the
collections.namedtuple function. Here's an example:
from collections import namedtuple # create a named tuple with two fields: 'x' and 'y' Point = namedtuple('Point', ['x', 'y']) # create an instance of the named tuple p = Point(1, 2) # access fields by index print(p) # prints 1 # access fields by name print(p.x) # prints 1 print(p.y) # prints 2
Named tuples are similar to regular tuples, but with the added benefit of having named fields that can make the code more readable and self-documenting. They are immutable, like regular tuples, so their fields cannot be changed once they are created.
Tuple Inside a List in Python
In some cases we need to make a list of tuples, for example for grouping related data. So you can create a list that contains tuples using the following syntax:
my_list = [(item1, item2), (item3, item4), (item5, item6)]
Each tuple in the list can contain multiple items, which can be of any data type, including strings, numbers, and other tuples. Here's an example of a list containing tuples with different data types:
my_list = [("apple", 2), ("orange", 3), ("banana", 4.5), ("grape", ("red", "green"))]
You can access the items in a tuple inside a list using indexing. For example, to access the second item in the first tuple in the list above, you can use the following code:
print(my_list) # Output: 2
Appending a Tuple
To append a tuple to a list in Python, you can use the
append() method. Here's an example:
my_list = [(1, 2), (3, 4)] new_tuple = (5, 6) my_list.append(new_tuple) print(my_list) # Output: [(1, 2), (3, 4), (5, 6)]