
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
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
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.
- Related Articles
- Python – Trim tuples by K
- Python – Filter consecutive elements Tuples
- N element incremental tuples in Python
- Chunk Tuples to N in Python
- Repeating tuples N times in Python
- Find Dissimilar Elements in Tuples in Python
- Python – Extract tuples with elements in Range
- Python program to find Tuples with positive elements in List of tuples
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Remove tuples from list of tuples if greater than n in Python
- Extract tuples having K digit elements in Python
- Python program to find Tuples with positive elements in a List of tuples
- Sort Tuples by Total digits in Python
- Python – Sort Tuples by Total digits
- Python program to replace first ‘K’ elements by ‘N’

Advertisements