Trim tuples by N elements in Python

When it is required to trim a list of tuples by a specific number of elements, the ‘del’ operator can be used.

Below is a demonstration of the same −

Example

Live Demo

my_list = [(1,2, 11), (99, 76, 34, 89), (3.08, 11.56), ("Hi", "Will"), ("Rob", 'Ron')]
n = 2
print("The list is :")
print(my_list)
print("The value of N is")
print(n)
del my_list[n]
print("The list after deleting N elements is :")
print(my_list)

Output

The list is :
[(1, 2, 11), (99, 76, 34, 89), (3.08, 11.56), ('Hi', 'Will'), ('Rob', 'Ron')]
The value of N is
2
The list after deleting N elements is :
[(1, 2, 11), (99, 76, 34, 89), ('Hi', 'Will'), ('Rob', 'Ron')]

Explanation

• A list of tuple is defined, and is displayed on the console.

• The number by which the tuple has to be trimmed, n, is defined.

• It is displayed on the console.

• The ‘del’ operator is used with the ‘n’ and the specific number of elements are deleted.

• The list, after deleting specific elements is displayed on the console.