Remove tuple from list python – Python Program to Remove Elements from a Tuple

Remove tuple from list python: In the previous article, we have discussed Python Program to Check if all Characters of String are Alphanumeric or Not
Tuple in Python:

A tuple is an immutable list of objects. That means the elements of a tuple cannot be modified or changed while the program is running.

The elements from any tuple can be removed by slicing the tuple.

Slicing:

Slicing is a Python feature that allows you to access chunks of sequences such as strings, tuples, and lists. You may also use them to change or remove elements from changeable sequences like lists. Slices can also be used on third-party objects such as NumPy arrays, Pandas series, and data frames.

Slicing allows for the creation of code that is clear, concise, and readable.

Given a tuple, the task is to remove elements from a given tuple.

Examples:

Example 1:

Input:

Given Tuple = (14, 32, 16, 85, 47, 65)

Output:

The above given tuple after removal of { 3 } Element =  (14, 32, 16, 47, 65)

Example 2:

Input:

Given Tuple = (12, 3, 48, 98, 23, 64, 75, 10, 56)

Output:

The above given tuple after removal of { 5 } Element =  (12, 3, 48, 98, 23, 75, 10, 56)

Program to Remove Elements from a Tuple

Below are the ways to remove Elements from a Given Tuple

Method: Using Slicing Operator (Static input)

Approach:

  • Give the tuple as static input and store it in a variable.
  • Give the number as static input and store it in another variable.
  • Do out the Slicing from ‘0 ‘ to given ‘n-1’ and store it in a variable.
  • Again do out the Slicing from ‘n+1’ to the given length of the tuple (till the end) and store it in another variable.
  • Add the above two slicing parts using the ‘+’ operator and store them in a variable.
  • Print the final tuple after removal of given elements from the above-given tuple.
  • The Exit of the program.

Below is the implementation:

# Give the tuple as static input and store it in a variable.
gven_tup = (14, 32, 16, 85, 47, 65)
# Give the number as static input and store it in another variable.
num = 3
# Do out the Slicing from '0 ' to given 'n-1' and store it in a variable.
fst_part = gven_tup[:num]
# Again do out the Slicing from 'n+1' to given length of tuple (till end) and
# store it in another variable.
secnd_part = gven_tup[num+1:]
# Add the above two slicing parts using '+' operator and store it in a variable.
gven_tup = fst_part + secnd_part
# Print the final tuple after removal of given elements from the above given tuple.
print(
    "The above given tuple after removal of {", num, "} Element = ", gven_tup)

Output:

The above given tuple after removal of { 3 } Element =  (14, 32, 16, 47, 65)

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.