![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Advertisements