In this article Tuples in Python we give the information about tuples are a data structure used in programming. It is similar to list but in this the data is stored in immutable form. This means that once a tuple is created, you cannot change, add, or remove its elements.
Tuples in Python:
Features of tuple:
1. Immutable: The elements of a tuple cannot be changed.
2. Ordered: The elements in a tuple are in a certain order.
3. Mixed Data Types: Tuples can contain different types of data (e.g., numbers, strings, etc.).
4. Duplicate Elements: A tuple may contain duplicate elements.
Creation of Tuples
Tuple is an immutable data type in Python, which means it cannot be changed after it is created.
Tuple is created by writing data inside () (parentheses).
Syntax:
my_tuple = (value1, value2, value3, …)
Example:
# Creating empty tuple
empty_tuple = ()
# Tuple with a single element (note that comma is required)
single_element_tuple = (5,)
#Tuple with multiple data types
mixed_tuple = (1,”Python”, 3.5)
#Tuple can also be created from list
list_data = [1, 2, 3]
tuple_from_list = tuple(list_data)
# to print
print(empty_tuple) # Output: ()
print(single_element_tuple) # Output: (5,)
print(mixed_tuple) # Output: (1, ‘Python’, 3.5)
print(tuple_from_list) # Output: (1, 2, 3)
Accessing Tuple Values
Values in a tuple can be accessed using their indexes.
- Indexing starts from 0.
- You can use both positive and negative indexing.
Example:
# Creating tuple
my_tuple = (10, 20, 30, 40, 50)
# Positive Indexing
print(my_tuple[0]) # Output: 10 (first element)
print(my_tuple[3]) # Output: 40 (fourth element)
# Negative Indexing (counting from the last)
print(my_tuple[-1]) # Output: 50 (last element)
print(my_tuple[-3]) # Output: 30 (3rd element)
Error Handling:
If you use an index that does not exist in the Tuple, an IndexError will occur.
print(my_tuple[5]) # IndexError: tuple index out of range
Accessing Nested Values inside Tuples (Nested Tuples)
There can be another Tuple inside a Tuple. This is called nested tuple.
Example:
nested_tuple = (1, (2, 3), (4, 5, 6))
# Accessing the values of the outer tuple
print(nested_tuple[1]) # Output: (2, 3)
# Accessing values of inner tuple
print(nested_tuple[1][0]) # Output: 2
print(nested_tuple[2][2]) # Output: 6
Accessing via Loops
A for loop can be used to access each element of a tuple one by one.
Example:
my_tuple = (10, 20, 30, 40)
for value in my_tuple:
print(value)
#Output:
#10
#20
#30
#40
Conclusion:
- Creation of tuples is easy and fast.
- Values can be accessed with the help of indexing and loops.
- Tuples can also contain nested structures.
Updating Tuples:
Tuples are immutable in Python, which means that once a tuple is created, no changes (such as addition, deletion, or replacement) can be made to it directly.
However, you can update the Tuple indirectly using some methods.
- Adding Elements to a Tuple
Elements cannot be added directly to a Tuple, but you can create a new Tuple by combining an old Tuple with a new Tuple.
Example:
my_tuple = (1, 2, 3)
# Create a new Tuple to add new elements
my_tuple = my_tuple + (4, 5)
print(my_tuple) # Output: (1, 2, 3, 4, 5)
- Modifying an Element of Tuple
Because Tuple is immutable, you cannot change any particular element directly.
But if there is mutable data (such as list) inside the Tuple, then that mutable data can be changed.
Example:
#Tuple with mutable data
my_tuple = (1, 2, [3, 4])
# Changing element of list
my_tuple[2][0] = 99
print(my_tuple) # Output: (1, 2, [99, 4])
- Deleting elements of a tuple and creating a new tuple
It is not possible to directly delete an element of a tuple. But you can create a new Tuple using slicing and concatenation.
Example:
my_tuple = (1, 2, 3, 4, 5)
# Removing the second and fourth elements
my_tuple = my_tuple[:1] + my_tuple[2:]
print(my_tuple) # Output: (1, 3, 4, 5)
-
Replacing the Tuple completely
If you want to replace the entire Tuple, you can assign a new Tuple with the same name.
Example:
my_tuple = (1, 2, 3)
# Assign new Tuple
my_tuple = (4, 5, 6)
print(my_tuple) # Output: (4, 5, 6)
- Converting Tuple to List and then Updating
If the Tuple needs to be changed, first convert it to a list, update it, and then convert it back to a Tuple.
Example:
my_tuple = (1, 2, 3)
# Convert Tuple to List
temp_list = list(my_tuple)
# Update the list
temp_list[1] = 99
# Convert List back to Tuple
my_tuple = tuple(temp_list)
print(my_tuple) # Output: (1, 99, 3)
Conclusion:
- Tuple cannot be updated directly because it is immutable.
- But you can update the Tuple indirectly by using slicing, concatenation, or mutable data structures.
- If frequent changes are required, it is better to use list.
Deleting Tuple Elements:
Tuple is an immutable data structure, so you cannot directly delete a particular element of a Tuple. However, it is possible to delete the entire tuple. Some alternative methods exist to remove or replace elements of a tuple.
- Deleting the Entire Tuple
You can delete an entire tuple using Python’s del keyword.
Example:
my_tuple = (1, 2, 3, 4, 5)
# Delete the entire tuple
del my_tuple
# Now accessing the Tuple will cause error
# print(my_tuple) # Output: NameError: name ‘my_tuple’ is not defined
- Creating a new Tuple to remove some elements of the Tuple.
To remove a particular element of a tuple, you can create a new tuple using slicing and concatenation.
Example:
my_tuple = (1, 2, 3, 4, 5)
# Removing the second and fourth elements
new_tuple = my_tuple[:1] + my_tuple[2:3] + my_tuple[4:]
print(new_tuple) # Output: (1, 3, 5)
-
Removing elements by converting Tuple to List
You can remove elements by converting Tuple to List. After this, it can be converted back to Tuple.
Example:
my_tuple = (1, 2, 3, 4, 5)
# Convert Tuple to List
temp_list = list(my_tuple)
# Remove element from list
removing temp_list.remove(3) #3
# Convert List back to Tuple
my_tuple = tuple(temp_list)
print(my_tuple) # Output: (1, 2, 4, 5)
- Removing elements of nested list from tuple (if tuple contains mutable data)
If there is a mutable data structure (such as a List) inside the Tuple, you can delete elements of that List.
Example:
my_tuple = (1, 2, [3, 4, 5])
# Delete elements of nested list
my_tuple[2].remove(4)
print(my_tuple) # Output: (1, 2, [3, 5])
Conclusion:
- It is not possible to directly delete any particular element of a Tuple because Tuple is immutable.
- The entire tuple can be deleted using del keyword.
- Removing some elements of a tuple can be done by slicing, concatenation, or converting the tuple to a list.
- Elements of a mutable data structure (such as a List) in a nested tuple can be changed or removed.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining