This is a Python Program to remove all tuples in a list of tuples with the USN outside the given range.
The program removes all tuples in a list of tuples with the USN outside the given range.
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.
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)
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.
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.
- Apply for Python Internship
- Check Information Technology Books
- Practice Programming MCQs
- Apply for Programming Internship
- Check Python Books