In the previous two chapters, we discussed the tuples in Python, and we know probably now about Python Tuples immutable behaviour. This chapter will discuss how you can insert, modify, or delete elements from a tuple. Ideally, you cannot perform these operations on a tuple due to its immutable characteristics. But where there’s a will, there’s always a way. We will look at some workarounds to understand how you can perform these operations on tuples.
Let us break the Python Tuples Immutable Behaviour
How to Insert elements in a Tuple?
As mentioned in the earlier chapters in the introduction section of this post, the tuples are immutable. It means that we cannot update the tuple elements after it is created. But we can achieve it by following the workaround steps mentioned below.
- Copy all the elements in the tuple till your desired position into a temporary variable.
- Concatenate the new element to the desired position to the temporary variable.
- Concatenate the remaining elements from the primary tuple variable to the temporary variable and eventually save this concatenation with the same old variable.
I know that this will sound confusing to you. It did for me as well, and I understood this logic better with the help of a program. Let us, therefore, have a look at the program mentioned below to understand this logic.
#Python program to insert an element in a tuple
players = ('Dhoni','Sachin','Sourav','Lakshman','Dravid')
print("The original players tuple data: \n{}".format(players))
#accept new player and the desired position
newPlayerlst = [input("Enter the new player name: ")]
newPlayer = tuple(newPlayerlst)
position = int(input("Enter the desired position no: "))
#copy the players till the desired position to a temporary tuple
temp = players[0:position-1]
#concatenate the new player at the last positon of temporary tuple
temp = temp + newPlayer
#concatenate the remaining players till the end
players = temp + players[position-1:]
print("The new tuple with new player is: \n{}".format(players))
Output
The original players tuple data:
('Dhoni', 'Sachin', 'Sourav', 'Lakshman', 'Dravid')
Enter the new player name: Samson
Enter the desired position no: 1
The new tuple with new player is:
('Samson', 'Dhoni', 'Sachin', 'Sourav', 'Lakshman', 'Dravid')
How to modify Elements in a tuple?
Yes, the tuples are immutable. But we had a look at how we can insert a new element to a tuple in the program mentioned above. Similarly, we can also modify the component of a tuple by following the logic discussed below.
- Copy all the elements in the tuple till your desired position into a temporary variable.
- Concatenate the new element to the desired position to the temporary variable.
- Concatenate the remaining elements from the primary tuple variable to the temporary variable by removing the component that needs to be removed and eventually save this concatenation with the same old variable.
Let us look at a program now to see how we can accomplish this logic.
#Python program to modify an element in a tuple
teams = ("Chennai Super Kings","Mumbai Indians","Delhi Capitals","Sunrisers Hyderabad","Gujarat Titans")
print("The original teams tuple: \n{}".format(teams))
#accept a new team and its position number
newTeamLst = [input("Enter new team name: ")]
newTeam = tuple(newTeamLst)
position = int(input("Enter the desired position: "))
#copy the teams till the desired position to a temporary tuple
temp = teams[0:position-1]
#concatenate the new team at the last positon of temporary tuple
temp = temp + newTeam
#concatenate the remaining teams till the end
#by removing a team at the chosen location
teams = temp + teams[position:]
print("The updated tuple with the new team is: \n{}".format(teams))
Output
The original teams tuple:
('Chennai Super Kings', 'Mumbai Indians', 'Delhi Capitals', 'Sunrisers Hyderabad', 'Gujarat Titans')
Enter new team name: Rajasthan Royals
Enter the desired position: 1
The updated tuple with the new team is:
('Rajasthan Royals', 'Mumbai Indians', 'Delhi Capitals', 'Sunrisers Hyderabad', 'Gujarat Titans')
How to delete elements from a tuple?
If we can insert and modify elements from a tuple by following a different way, we can delete an element from a tuple. We can do it by following the logic mentioned below.
- Copy all the elements in the tuple till your desired position into a temporary variable.
- Concatenate the remaining elements from the primary tuple variable to the temporary variable by removing the part that needs to be removed and eventually save this concatenation with the same old variable.
We will now look at a program to understand how to achieve this deletion.
#Python program to delete element in a tuple
emotions = ("Happiness","Suffering","Anger","Love")
print("The original emotions tuple: \n{}".format(emotions))
#accept the position of the emotion to be deleted
position = int(input("Enter the position number of the emotion to be deleted: "))
#copy the emotions till the desired position to a temporary tuple
temp = emotions[0:position-1]
#concatenate the remaining emotions till the end
#by removing the emotion at the chosen location
emotions = temp + emotions[position:]
print("The emotions tuple after deletion: \n{}".format(emotions))
Output
The original emotions tuple:
('Happiness', 'Suffering', 'Anger', 'Love')
Enter the position number of the emotion to be deleted: 2
The emotions tuple after deletion:
('Happiness', 'Anger', 'Love')
We hope you understand how to break the Python Tuples Immutable behaviour.