![]() The outer ‘for’ loop will iterate based on the number of inner lists defined in the main list. Here, the nested ‘for’ loops have used to sort the items of the nested list. An empty list has been declared to store the values of the sorted list. Three sorted lists of the tuples have shown in the output based on the sorted position.Įxample-3: Sort a list containing another listĬreate a python file with the following script to sort the nested list using lambda. The following output will appear after executing the above script. Print ( "The sorted list based on the third item: \n", sorted_list3 ) Output: Sorted_list3 = sorted (tuple_list, key = lambda x: x ) # Sort the list based on the third item of the tuple Print ( "The sorted list based on the second item: \n", sorted_list2 ) Sorted_list2 = sorted (tuple_list, key = lambda x: x ) # Sort the list based on the second item of the tuple Print ( "The sorted list based on the first item: \n", sorted_list1 ) Sorted_list1 = sorted (tuple_list, key = lambda x: x ) ![]() # Sort the list based on the first item of the tuple This will sort the list based on the third item of each tuple. The sorting position is set to 2 in the third sorted function. This will sort the list based on the second item of each tuple. The sorting position is set to 1 in the second sorted function. This will sort the list based on the first item of each tuple. The sorting position is set to 0 in the first sorted function. Three types of sorting have been shown in the script. The following output will appear after executing the above script.Ĭreate a python file with the following script to sort a list of three tuples using lambda, where each tuple contains three items. Print ( "The list of the sorted values are:" ) Sorted_list = sorted (n_list, key = lambda x: int (x ) ) ![]() # Sort the list using lambda and sorted function # Declare a list of string with number values Next, the print() function has used to print the sorted list with space. The list variable has been given as the first argument value, the lambda has been set as the key value, and the starting position of the sort has been set in the third argument. Here, lambda has used inside the sorted() function to sort the list. A list of 6 items has been defined in the script. Example-1: Sort a list of numerical string dataĬreate a python file with the following script to sort a list of string data using lambda that contains all number values. The uses of the lambda function for sorting the list and dictionary objects in different ways have been explained in this tutorial. The coder can define the sorting order based on the requirement by using this function. Many built-in functions exist in Python to sort the list of data in ascending or descending order. The values of the tuple and list are accessed by numeric index, and the keys access the values of the dictionary. Tuple, list, and dictionary are used in Python to store multiple data. The sort operation is applied to a list of data in any programming language.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |