Sort list of lists ascending and then descending in python

Sort list of lists ascending and then descending in python

To sort a list of lists in ascending and then descending order in Python, you can use the sorted() function along with a custom key function that specifies how to sort the sublists. Here's an example:

# Sample list of lists data = [[3, 1], [1, 2], [2, 3], [4, 5], [5, 4]] # Sort in ascending order based on the first element of each sublist ascending_sorted = sorted(data, key=lambda x: x[0]) # Sort in descending order based on the second element of each sublist descending_sorted = sorted(data, key=lambda x: x[1], reverse=True) print("Ascending Sorted:") print(ascending_sorted) print("Descending Sorted:") print(descending_sorted) 

In this example:

  1. We have a list of lists named data, where each sublist contains two elements.

  2. To sort in ascending order based on the first element of each sublist, we use sorted(data, key=lambda x: x[0]). This sorts the sublists based on their first element.

  3. To sort in descending order based on the second element of each sublist, we use sorted(data, key=lambda x: x[1], reverse=True). The reverse=True argument reverses the sorting order.

The output will be:

Ascending Sorted: [[1, 2], [2, 3], [3, 1], [4, 5], [5, 4]] Descending Sorted: [[4, 5], [5, 4], [2, 3], [1, 2], [3, 1]] 

You can adapt this code to sort your list of lists based on specific criteria or customize the key functions accordingly.

Examples

  1. How to sort a list of lists in ascending order by the first element in Python?

    • Description: This query demonstrates sorting a list of lists by the first element in ascending order.
    • Code:
      # List of lists data = [[3, 'C'], [1, 'A'], [2, 'B']] # Sort by the first element sorted_data = sorted(data, key=lambda x: x[0]) print(sorted_data) 
  2. How to sort a list of lists in descending order by the first element in Python?

    • Description: This query involves sorting a list of lists by the first element in descending order.
    • Code:
      # Sort by the first element in descending order sorted_data_desc = sorted(data, key=lambda x: x[0], reverse=True) print(sorted_data_desc) 
  3. How to sort a list of lists by the second element in ascending order in Python?

    • Description: This query focuses on sorting by the second element in ascending order.
    • Code:
      # Sort by the second element sorted_by_second = sorted(data, key=lambda x: x[1]) print(sorted_by_second) 
  4. How to sort a list of lists by multiple elements in ascending order in Python?

    • Description: This query demonstrates sorting by multiple elements in ascending order.
    • Code:
      data = [[3, 'C', 10], [1, 'A', 20], [2, 'B', 15]] # Sort by the first element, then the second element sorted_by_multiple = sorted(data, key=lambda x: (x[0], x[1])) print(sorted_by_multiple) 
  5. How to sort a list of lists by multiple elements in descending order in Python?

    • Description: This query demonstrates sorting by multiple elements in descending order.
    • Code:
      # Sort by the third element in descending order, then the first element sorted_by_descending = sorted(data, key=lambda x: (x[2], x[0]), reverse=True) print(sorted_by_descending) 
  6. How to sort a list of lists by length in ascending order in Python?

    • Description: This query focuses on sorting a list of lists based on the length of the inner lists.
    • Code:
      data = [[1, 2, 3], [1], [1, 2], [1, 2, 3, 4]] # Sort by the length of inner lists sorted_by_length = sorted(data, key=len) print(sorted_by_length) 
  7. How to sort a list of lists with a custom key in ascending order in Python?

    • Description: This query demonstrates sorting with a custom key function for more complex logic.
    • Code:
      # Custom key: sort by the sum of the elements in each list sorted_custom = sorted(data, key=lambda x: sum(x)) print(sorted_custom) 
  8. How to sort a list of lists by specific index in ascending order in Python?

    • Description: This query demonstrates sorting by a specific index of inner lists.
    • Code:
      data = [[1, 10], [2, 5], [3, 7]] # Sort by the second element of each inner list sorted_by_index = sorted(data, key=lambda x: x[1]) print(sorted_by_index) 
  9. How to sort a list of lists in ascending order with custom logic in Python?

    • Description: This query focuses on sorting with custom logic for additional control.
    • Code:
      # Custom logic: sort by the product of the first two elements sorted_with_custom_logic = sorted(data, key=lambda x: x[0] * x[1]) print(sorted_with_custom_logic) 
  10. How to sort a list of lists with stability in ascending order in Python?

    • Description: This query demonstrates stable sorting, preserving the original order for lists with the same sorting key.
    • Code:
      # Use 'mergesort' for stable sorting sorted_with_stability = sorted(data, key=lambda x: x[1], kind='mergesort') print(sorted_with_stability) 

More Tags

ruby-hash qfiledialog interrupt poker tostring ios8 led grid-layout completable-future android-gps

More Python Questions

More Animal pregnancy Calculators

More Housing Building Calculators

More Other animals Calculators

More Pregnancy Calculators