Python: Sort a list of lists by a given index of the inner list
Sort List of Lists by Index
Write a Python program to sort a list of lists by a given index of the inner list.
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Original list: [('Greyson Fulton', 98, 99), ('Brady Kent', 97, 96), ('Wyatt Knott', 91, 94), ('Beau Turnbull', 94, 98)] Sort the said list of lists by a given index ( Index = 0 ) of the inner list [('Beau Turnbull', 94, 98), ('Brady Kent', 97, 96), ('Greyson Fulton', 98, 99), ('Wyatt Knott', 91, 94)] Sort the said list of lists by a given index ( Index = 2 ) of the inner list [('Wyatt Knott', 91, 94), ('Brady Kent', 97, 96), ('Beau Turnbull', 94, 98), ('Greyson Fulton', 98, 99)]
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Python program to sort a list of dictionaries by a key.
- Write a Python program to sort a list of tuples by the second element.
- Write a Python program to sort a nested list based on multiple indices.
- Write a Python program to sort a list of lists in reverse order by an index.
Go to:
Previous: Write a Python program to check if the elements of a given list are unique or not.
Next: Write a Python program to remove all elements from a given list present in another list.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.