Sorting list of lists by the first element of each sub-list in python

Sorting list of lists by the first element of each sub-list in python

You can sort a list of lists by the first element of each sub-list using the sorted() function and a custom sorting key. Here's how you can do it:

data = [ [3, "apple"], [1, "banana"], [2, "orange"], [4, "grape"] ] sorted_data = sorted(data, key=lambda x: x[0]) print(sorted_data) 

In this example, the sorted() function takes the data list and uses the key parameter to specify a custom sorting key. The lambda function lambda x: x[0] extracts the first element of each sub-list, which is then used as the sorting criterion.

After sorting, the sorted_data list will be:

[[1, 'banana'], [2, 'orange'], [3, 'apple'], [4, 'grape']] 

This arrangement shows that the sub-lists are now sorted based on their first elements in ascending order. If you want to sort in descending order, you can use the reverse parameter:

sorted_data_descending = sorted(data, key=lambda x: x[0], reverse=True) 

The reverse=True argument reverses the order of sorting.

Examples

  1. "Python sort list of lists by first element" Description: This query is likely used by someone looking to sort a list of lists in Python based on the first element of each sublist.

    # Code: list_of_lists = [[3, 'apple'], [1, 'banana'], [2, 'orange']] sorted_list = sorted(list_of_lists, key=lambda x: x[0]) print(sorted_list) 
  2. "Python sort nested lists by first element" Description: This query targets individuals who want to sort nested lists by the first element of each sublist.

    # Code: nested_list = [[3, 'apple'], [1, 'banana'], [2, 'orange']] nested_list.sort(key=lambda x: x[0]) print(nested_list) 
  3. "Python sort list of tuples by first element" Description: This query is for those who wish to sort a list of tuples based on the first element of each tuple.

    # Code: list_of_tuples = [(3, 'apple'), (1, 'banana'), (2, 'orange')] sorted_list = sorted(list_of_tuples, key=lambda x: x[0]) print(sorted_list) 
  4. "Python sorting lists of lists by first element descending" Description: This query targets users looking to sort a list of lists in descending order based on the first element of each sublist.

    # Code: list_of_lists = [[3, 'apple'], [1, 'banana'], [2, 'orange']] sorted_list = sorted(list_of_lists, key=lambda x: x[0], reverse=True) print(sorted_list) 
  5. "Python sort list of lists by element at index 0" Description: This query is for those wanting to sort a list of lists based on the element at index 0 of each sublist.

    # Code: list_of_lists = [[3, 'apple'], [1, 'banana'], [2, 'orange']] sorted_list = sorted(list_of_lists, key=lambda x: x[0]) print(sorted_list) 
  6. "Python sort list of lists by first element alphabetically" Description: This query is for sorting a list of lists alphabetically based on the first element of each sublist.

    # Code: list_of_lists = [['orange', 2], ['apple', 3], ['banana', 1]] sorted_list = sorted(list_of_lists) print(sorted_list) 
  7. "Python sorting list of lists by first element without lambda" Description: This query suggests that the user wants to sort a list of lists without using lambda functions.

    # Code: from operator import itemgetter list_of_lists = [[3, 'apple'], [1, 'banana'], [2, 'orange']] sorted_list = sorted(list_of_lists, key=itemgetter(0)) print(sorted_list) 
  8. "Python sort list of lists by first element of each sublist" Description: This query is straightforward, indicating the user wants to sort a list of lists by the first element of each sublist.

    # Code: list_of_lists = [[3, 'apple'], [1, 'banana'], [2, 'orange']] list_of_lists.sort(key=lambda x: x[0]) print(list_of_lists) 
  9. "Python sort list of lists by first element efficiently" Description: This query indicates the user is looking for an efficient method to sort a list of lists by the first element of each sublist.

    # Code: list_of_lists = [[3, 'apple'], [1, 'banana'], [2, 'orange']] list_of_lists.sort() print(list_of_lists) 
  10. "Python sort list of lists by first element in ascending order" Description: This query specifies sorting a list of lists by the first element of each sublist in ascending order.

    # Code: list_of_lists = [[3, 'apple'], [1, 'banana'], [2, 'orange']] list_of_lists.sort(key=lambda x: x[0]) print(list_of_lists) 

More Tags

aem getlatest execute git symlink maxlength mysql-json httponly statusbar adb

More Python Questions

More Cat Calculators

More Trees & Forestry Calculators

More Gardening and crops Calculators

More Bio laboratory Calculators