Python Program to Replace Elements in a Tuple


In this article, you will find out how to use Python to find the first and last elements of a tuple in this tutorial.

The quick response is that you can access the first and last elements of the tuple in Python using the index operator([]). The methods described here can be used to obtain your single tuple element.

Before diving in to the solution let us understand tuple in python.

What is tuple in Python?

A tuple is one of Python's four built-in data types for storing data collections. The other three are list, set, and dictionary, each with a unique set of features and applications.

The elements of a tuple are immutable and ordered. We can create a tuple by specifying the elements within the round brackets separated by commas, as shown below –

Example

firstuple = ("apple", "banana", "cherry")
print(firstuple)

Output

Following is the output of the above query –

('apple', 'banana', 'cherry')

Features of tuple in Python

Tuple items − Duplicate values are permitted for triple items, which are ordered and immutable. The first item in a triple has an index of [0], the second has an index of [1], and so on.

Ordered − When we say that a tuple is ordered, we are referring to the fact that the items are in a specific order that won't change.

Unchangeable − Tuples are immutable, which means that once we create a tuple, we cannot change, add, or remove any of its components.

Replacing the elements in a tuple

Tuples are immutable i.e. once we create a tuple we cannot change their values. Therefore to change the elements in a tuple -

  • We'll turn the tuple into a list.

  • Refresh the list's necessary items.

  • Return the list to the tuple form, then assign it to the original tuple.

Example

Let us see an example for this. Here we are creating a tuple, converting it into a list and printing the results.

tuple1 = (1,2,3,4,5,6,7)
list1 = list(tuple1)
list1[5] = 9
tuple1 = tuple(list1)
print(tuple1)

Output

(1,2,3,4,5,9,7)

Example

Now let us see the same example with sting values –

x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)

Output

("apple", "kiwi", "cherry")

Deleting an element from a tuple

In order to remove an element from the beginning of a tuple, we will make a new tuple with the remaining elements as shown below.

myTuple = (11, 22, 33, 44, 55, 66, 77, 88)
print("Original Tuple is:", myTuple)
myTuple = myTuple[1:]
print("Updated tuple is:", myTuple)

Output

Original Tuple is: (11, 22, 33, 44, 55, 66, 77, 88)
The updated tuple is: (22, 33, 44, 55, 66, 77, 88)

Example

We can slice out the remaining elements of a tuple in the event that the last element needs to be removed.

myTuple = (1, 2, 3, 4, 5, 6, 7, 8)
print("Original Tuple is:", myTuple)
tupleLength = len(myTuple)
myTuple = myTuple[0:tupleLength - 1]
print("Updated tuple is:", myTuple)

Output

Original Tuple is: (1, 2, 3, 4, 5, 6, 7, 8)
Updated tuple is: (1, 2, 3, 4, 5, 6, 7)

Example

To remove an element that is present at index "i". We will cut the original tuple into two slices, the initial slice will include each element in the initial tuple from index 0 to index i1.

The items from index "i 1" to last will be in the second tuple. Then, excluding the element at index "i," we will concatenate the freshly created tuples as follows –

myTuple = (1, 2, 3, 4, 5, 6, 7, 8)
print("Original Tuple is:", myTuple)
left_tuple = myTuple[0:3]
right_tuple = myTuple[4:]
myTuple = left_tuple + right_tuple
print("Updated tuple after deleting element at index 3 is:", myTuple)

Output

Original Tuple is: (1, 2, 3, 4, 5, 6, 7, 8)
Updated tuple after deleting element at index 3 is: (1, 2, 3, 5, 6, 7, 8)

Modifying an element at a specific index

To modify element from an index we will make two slices of the original tuple to change the element at index "i" of the tuple.

The first slice will have components from the initial tuple's index 0 to i-1. From index "i 1" to the last, the second slice will include all the components. The element at index "i" will then be updated by inserting the new value at the following position.

Example

myTuple = (9, 8, 7, 6, 5, 4, 3, 2, 1)
print("Original Tuple is:", myTuple)
left_tuple = myTuple[0:2]
right_tuple = myTuple[5:]
myTuple = left_tuple + (100,) + right_tuple
print("Updated tuple after modifying element at index 3 is:", myTuple)

Output

Original Tuple is: (9, 8, 7, 6, 5, 4, 3, 2, 1)
Updated tuple after modifying element at index 3 is: (9, 8, 100, 4, 3, 2, 1)

Updated on: 20-Feb-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements