- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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 –
firstuple = ("apple", "banana", "cherry") print(firstuple)
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 , the second has an index of , 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.
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 = 9 tuple1 = tuple(list1) print(tuple1)
Now let us see the same example with sting values –
x = ("apple", "banana", "cherry") y = list(x) y = "kiwi" x = tuple(y) print(x)
("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)
Original Tuple is: (11, 22, 33, 44, 55, 66, 77, 88) The updated tuple is: (22, 33, 44, 55, 66, 77, 88)
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)
Original Tuple is: (1, 2, 3, 4, 5, 6, 7, 8) Updated tuple is: (1, 2, 3, 4, 5, 6, 7)
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)
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.
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)
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)
- Related Articles
- Python Program to Replace Elements in a List
- Python Program to add elements to a Tuple
- Python Program to print elements of a tuple
- Replace duplicates in tuple in Python
- Python program to get first and last elements from a tuple
- Python program to replace first ‘K’ elements by ‘N’
- Java Program to Replace Elements in a List
- Golang program to replace elements in a slice
- C++ Program to Replace Elements in a Dictionary
- Swift Program to Replace Elements in a Dictionary
- Python Program to replace list elements within a range with a given number
- Python - Join tuple elements in a list
- How to append elements in Python tuple?
- Delete Tuple Elements in Python
- Python program to count the elements in a list until an element is a Tuple?