Write a Python program to Sort lists in tuple


This program sorts each list contained within a tuple and returns a new tuple with the sorted lists. Here's what happens step-by-step:

  • The val tuple is defined and initialized with 3 lists and 1 tuple.
  • The original val tuple is printed using the following line:
    • print("Original Tuple is : " , val)
  • The res tuple is created by using a generator expression in the tuple function. The generator expression sorts each list in the val tuple using the sorted function and returns an iterator that yields the sorted lists. The tuple function then creates a new tuple with the sorted lists:
    • res = tuple((sorted(i) for i in val))
  • The res tuple is printed using the following line:
    • print("Tuple after sorting list : ",res)

So the program takes a tuple that contains lists and tuples, sorts each list contained within the tuple, and returns a new tuple with the sorted lists.

Source Code

val = ([10, 50, 60], [80, 20, 30], [70, 100, 40],(90,))
 
print("Original Tuple is : " , val)
 
res = tuple((sorted(i) for i in val))
print("Tuple after sorting list : ",res)

Output

Original Tuple is :  ([10, 50, 60], [80, 20, 30], [70, 100, 40], (90,))
Tuple after sorting list :  ([10, 50, 60], [20, 30, 80], [40, 70, 100], [90])

Example Programs