# Python | Sort lists in tuple

Sometimes, while working with Python tuples, we can have a problem in which we need to sort the tuples which constitutes of lists and we need to sort each of them. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using `tuple() + sorted()` + generator expression
This task can be performed using the combination of above functions. In this, we iterate through each list using generator expression and perform the sort operation using `sorted()`.

 `# Python3 code to demonstrate working of ` `# Sort lists in tuple ` `# Using tuple() + sorted() + generator expression ` ` `  `# Initializing tuple ` `test_tup ``=` `([``7``, ``5``, ``4``], [``8``, ``2``, ``4``], [``0``, ``7``, ``5``]) ` ` `  `# printing original tuple ` `print``(``"The original tuple is : "` `+` `str``(test_tup)) ` ` `  `# Sort lists in tuple ` `# Using tuple() + sorted() + generator expression ` `res ``=` `tuple``((``sorted``(sub) ``for` `sub ``in` `test_tup)) ` ` `  `# printing result ` `print``(``"The tuple after sorting lists : "` `+` `str``(res)) `

Output :

```The original tuple is : ([7, 5, 4], [8, 2, 4], [0, 7, 5])
The tuple after sorting lists : ([4, 5, 7], [2, 4, 8], [0, 5, 7])
```

Method #2 : Using `map() + sorted()`
This method performs the similar task as the above method, but it uses `map()` to extend the logic to each element of tuple, the task performed by list comprehension in above method.

 `# Python3 code to demonstrate working of ` `# Sort lists in tuple ` `# Using map() + sorted() ` ` `  `# Initializing tuple ` `test_tup ``=` `([``7``, ``5``, ``4``], [``8``, ``2``, ``4``], [``0``, ``7``, ``5``]) ` ` `  `# printing original tuple ` `print``(``"The original tuple is : "` `+` `str``(test_tup)) ` ` `  `# Sort lists in tuple ` `# Using map() + sorted() ` `res ``=` `tuple``(``map``(``sorted``, test_tup)) ` ` `  `# printing result ` `print``(``"The tuple after sorting lists : "` `+` `str``(res)) `

Output :

```The original tuple is : ([7, 5, 4], [8, 2, 4], [0, 7, 5])
The tuple after sorting lists : ([4, 5, 7], [2, 4, 8], [0, 5, 7])
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.