Python Program to Remove All Tuples in a List Outside the Given Range

This is a Python Program to remove all tuples in a list of tuples with the USN outside the given range.

Problem Description

The program removes all tuples in a list of tuples with the USN outside the given range.

Problem Solution

1. Take in the lower and upper roll number from the user.
2. Then append the prefixes of the USN’s to the roll numbers.
3. Using list comprehension, find out which USN’s lie in the given range.
4. Print the list containing the tuples.
5. Exit.

Program/Source Code

Here is source code of the Python Program to remove all tuples in a list of tuples with the USN outside the given range. The program output is also shown below.

y=[('a','12CS039'),('b','12CS320'),('c','12CS055'),('d','12CS100')]
low=int(input("Enter lower roll number (starting with 12CS):"))
up=int(input("Enter upper roll number (starting with 12CS):"))
l='12CS0'+str(low)
u='12CS'+str(up)
p=[x for x in y if x[1]>l and x[1]<u]
print(p)
Program Explanation

1. User must enter the upper and lower roll number.
2. The prefixes are then appended to the roll numbers using the ‘+’ operator to form the USN.
3. List comprehension is then used to find the USN’s within the upper and lower range.
4. The list containing USN’s in the given range is then printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter lower roll number (starting with 12CS):50
Enter upper roll number (starting with 12CS):150
[('c', '12CS055'), ('d', '12CS100')]

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.