{"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 Output:<\/strong><\/p>\n When sorting a list, all of the list’s elements are compared to one another. However, before comparing the entries, it will call the key function on each entry to determine which portion of the object will be compared.<\/p>\n In other words, when an element is passed to the key function, it returns the element that should be used to compare the elements in the list while sorting.<\/p>\n To sort a list of tuples by the second or ith item, we must include our custom comparator, i.e. key function, in the\u00a0 sort () function.<\/p>\n We use lambda function as key function.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n First, create a function that accepts a tuple and returns the second element.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\ngiventuple = [('Hello', 400), ('this', 500), ('is', 200), ('BTechGeeks', 100)]<\/pre>\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
\n
1)Key Function<\/h3>\n
2)Using lambda function<\/h3>\n
# given tuple\r\ngiventuple = [('Hello', 400), ('this', 500), ('is', 200), ('BTechGeeks', 100)]\r\n# using lambda function\r\ngiventuple.sort(key=lambda elem: elem[1])\r\n# print giventuple\r\nprint(giventuple)\r\n<\/pre>\n
[('BTechGeeks', 100), ('is', 200), ('Hello', 400), ('this', 500)]<\/pre>\n
3)Using custom function as comparator<\/h3>\n
def comparator(elementtuple):\r\n return elementtuple[1]\r\n\r\n\r\n# given tuple\r\ngiventuple = [('Hello', 400), ('this', 500), ('is', 200), ('BTechGeeks', 100)]\r\n# using lambda function\r\ngiventuple.sort(key=comparator)\r\n# print giventuple\r\nprint(giventuple)\r\n<\/pre>\n
[('BTechGeeks', 100), ('is', 200), ('Hello', 400), ('this', 500)]<\/pre>\n
\n