In this article we will discuss how to Append, Insert, Replace and Delete elements from a tuple in python.
In Python, tuples are immutable i.e. once created we can not change its contents. But sometimes we want to modify the existing tuple, in that case we need to create a new tuple with updated elements only from the existing tuple.
Let’s see how to insert, modify and delete the elements from tuple.
Append an element in Tuple at end
Suppose we have a tuple i.e.
# Create a tuple tupleObj = (12 , 34, 45, 22, 33 )
Now to append an element in this tuple, we need to create a copy of existing tuple and then add new element to it using + operator i.e.
# Append 19 at the end of tuple tupleObj = tupleObj + (19 ,)
We will assign the new tuple back to original reference, hence it will give an effect that new element is added to existing tuple.
Contents of tuple will be now,
(12, 34, 45, 22, 33, 19)
A new Element is appended at the end of tuple.
Insert an element at specific position in tuple
To insert an element at a specific index in the existing tuple we need to create a new tuple by slicing the existing tuple and copying contents from it.
Suppose we have a tuple i.e.
# Create a tuple tupleObj = (12 , 34, 45, 22, 33 )
As indexing starts from 0 in tuple, so to insert an element at index n in this tuple, we will create two sliced copies of existing tuple from (0 to n) and (n to end) i.e.
# Sliced copy containing elements from 0 to n-1 tupleObj[ : n] # Sliced copy containing elements from n to end tupleObj[n : ]
Now join these two sliced copies with new elements in between i.e.
n = 2 # Insert 19 in tuple at index 2 tupleObj = tupleObj[ : n ] + (19 ,) + tupleObj[n : ]
Now contents of tuple will be.
(12, 34, 19, 45, 22, 33, 19)
A new Element is inserted at index n.
Modify / Replace the element at specific index in tuple
To replace the element at index n in tuple we will use the same slicing logic as above, but we will slice the tuple from from (0 to n-1) and (n+1 to end) i.e.
# Sliced copy containing elements from 0 to n-1 tupleObj[ : n] # Sliced copy containing elements from n to end tupleObj[n + 1 : ]
None of the above sliced copies contains existing element at index n. Now join these two sliced copies with new elements in between i.e.
tupleObj = (12, 34, 19, 45, 22, 33, 19) n = 2 # Replace the element at index 2 to 'Test' tupleObj = tupleObj[ : n] + ('test' ,) + tupleObj[n + 1 : ]
Now contents of tuple will be.
(12, 34, 'test', 45, 22, 33, 19)
Element in index n is replaced now.
Delete an element at specific index in tuple
To delete the element at index n in tuple we will use the same slicing logic as above, but we will slice the tuple from from (0 to n-1) and (n+1 to end) i.e.
# Sliced copy containing elements from 0 to n-1 tupleObj[ : n] # Sliced copy containing elements from n to end tupleObj[n + 1 : ]
None of the above sliced copies contains existing element at index n. Now join these two sliced copies i.e.
tupleObj =(12, 34, 'test', 45, 22, 33, 19) n = 2 # Delete the element at index 2 tupleObj = tupleObj[ : n ] + tupleObj[n+1 : ]
Now contents of tuple will be.
(12, 34, 45, 22, 33, 19)
Element in index n is deleted now.
Complete example is as follows,
def main(): # Create a tuple from list by type casting tupleObj = (12 , 34, 45, 22, 33 ) print("****** Append an element in Tuple at end ******") print("Original Tuple : ", tupleObj) # Append 19 at the end of tuple tupleObj = tupleObj + (19 ,) print("Modified Tuple : ", tupleObj) print("******* Insert an element at specific index in tuple *******") print("Original Tuple : ", tupleObj) n = 2 # Insert 19 in tuple at index 2 tupleObj = tupleObj[ : n] + (19 ,) + tupleObj[n : ] print("Modified Tuple : ", tupleObj) print("******* Modify / Replace the element at specific index in tuple *******") print("Original Tuple : ", tupleObj) n = 2 # Replace the element at index 2 to 'Test' tupleObj = tupleObj[ : n] + ('test' ,) + tupleObj[n + 1 : ] print("Modified Tuple : ", tupleObj) print("******* Delete the element at specific index in tuple *******") print("Original Tuple : ", tupleObj) n = 2 # Delete the element at index 2 tupleObj = tupleObj[ : n ] + tupleObj[n+1 : ] print("Modified Tuple : ", tupleObj) if __name__ == '__main__': main()
Output:
****** Append an element in Tuple at end ****** Original Tuple : (12, 34, 45, 22, 33) Modified Tuple : (12, 34, 45, 22, 33, 19) ******* Insert an element at specific index in tuple ******* Original Tuple : (12, 34, 45, 22, 33, 19) Modified Tuple : (12, 34, 19, 45, 22, 33, 19) ******* Modify / Replace the element at specific index in tuple ******* Original Tuple : (12, 34, 19, 45, 22, 33, 19) Modified Tuple : (12, 34, 'test', 45, 22, 33, 19) ******* Delete the element at specific index in tuple ******* Original Tuple : (12, 34, 'test', 45, 22, 33, 19) Modified Tuple : (12, 34, 45, 22, 33, 19)
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.