{"id":4160,"date":"2021-04-27T19:00:42","date_gmt":"2021-04-27T13:30:42","guid":{"rendered":"https:\/\/python-programs.com\/?p=4160"},"modified":"2021-11-22T18:43:04","modified_gmt":"2021-11-22T13:13:04","slug":"python-how-to-sort-a-list-of-tuples-by-second-item","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-tuples-by-second-item\/","title":{"rendered":"Python: How to Sort a List of Tuples by second Item"},"content":{"rendered":"

Tuples are a type of variable that allows you to store multiple items in a single variable. Tuple is one of four built-in data types in Python that are used to store data collections. The other three are List, Set, and Dictionary, all of which have different qualities and applications. A tuple is a collection that is both ordered and immutable.<\/p>\n

The task is to write a Python program that sorts tuples by the second item in each tuple given a list of tuples.<\/p>\n

Examples:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

giventuple = [('Hello', 400), ('this', 500), ('is', 200), ('BTechGeeks', 100)]<\/pre>\n

Output:<\/strong><\/p>\n

[('BTechGeeks', 100), ('is', 200), ('Hello', 400), ('this', 500)]<\/pre>\n

Python program for Sorting a List of Tuples by the Second item<\/h2>\n