Do not forget to put commas between the elements of a list

myVar = [['blueberries','fruit','5.20'],
         ['bean sprouts','vegetable','9.25'],
         ['tulip','flower','8.93']]

Now you could use the sorted builtin as Andy Knight suggested in the comments and specify a key function to use for comparing items.

Sort list by price and get a copy of the sorted list

sorted(myVar, key=lambda x: float(x[2]))

Sort list by item name

sorted(myVar, key=lambda x: x[0])
Answer from Dima Chubarov on Stack Overflow
🌐
Note.nkmk.me
note.nkmk.me › home › python
Sort a 2D List in Python | note.nkmk.me
July 27, 2023 - To do this, you can use list comprehension to apply sorted() to each list. ... l_2d = [[20, 3, 100], [1, 200, 30], [300, 10, 2]] pprint.pprint(l_2d, width=20) # [[20, 3, 100], # [1, 200, 30], # [300, 10, 2]] pprint.pprint([sorted(l) for l in ...
Discussions

How would you sort a 2D list by row based on the first column using a separate list as a key
If you want to sort it by month, why not use a dictionary of lists, such that dict['Nov'] = [0.0, 0.0, 0.09, etc.]? More on reddit.com
🌐 r/learnpython
3
1
November 22, 2022
How to sort by the second element in a 2D list
array.sort(key=lambda L: L[1]) The sort function (of course) needs values to compare, so when it scans through the list, the values it “sees” correspond to L[1] for each sublist L, i.e. the numbers in your 2D list. As you noted, without that key function the list-to-list comparison is essentially just a lexicographical sort by the first element, unless there’s a tie, in which case it looks at the number. More on reddit.com
🌐 r/learnpython
5
4
February 20, 2021
Sorting 2D List in Python - Stack Overflow
Any elements in lists have to be separated by commas. Please don't use list as a variable as it is an inbuilt function ... Take a look at the 'sorted' builtin function and, in particular, the [optional] 'key' named parameter More on stackoverflow.com
🌐 stackoverflow.com
Sorting 2d Array
Posted by u/PhantomWizard2099 - 2 votes and 14 comments More on reddit.com
🌐 r/learnpython
14
2
June 28, 2023
🌐
YouTube
youtube.com › watch
Sorting two dimensional lists in Python - YouTube
How to sort 2d list by the second or third sub item in a lists. Great for making high score tables.
Published: January 16, 2017
🌐
Script Everything
scripteverything.com › home › python › how to sort 2d array in python: 1 line of code without numpy
How To Sort 2D Array In Python: 1 Line Of Code Without Numpy
July 29, 2021 - To sort a two-dimensional list ... for another) in Python, without using any imported libraries, use the built-in sort() list method and sorted() function....
🌐
Statistics Globe
statisticsglobe.com › home › python programming language for statistics & data science › sort 2d list in python (2 examples)
How to Sort 2D Python List | Order 2 Dimensional Array Elements
June 1, 2023 - For instance, to sort a 2D list based on the first element of each sublist, we defined the lambda function as lambda x: x[0]. The sorted() function and the sort() method are both used to sort elements in a list in Python, but they have some ...
🌐
Reddit
reddit.com › r/learnpython › how would you sort a 2d list by row based on the first column using a separate list as a key
r/learnpython on Reddit: How would you sort a 2D list by row based on the first column using a separate list as a key
November 22, 2022 -

so i have a data set of rain data over the course of a year where it ends up being formatted into something like this except its a lot longer

[['Nov', 0.0], ['Nov', 0.0], ['Nov', 0.09],['Feb', 0.0], ['Feb', 0.0], ['Feb', 0.09],['Oct', 0.56], ['Oct', 0.0], ['Oct', 0.03], ['Oct', 0.62]]

now I figured I would need another list to function as a lookup to help sort this data by month according to calendar order so I made one

month_lookup = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

but now I am a tad confused about how I would go about using something like the sorted() function to use the lookup as the way to sort the 2D list above

🌐
Delft Stack
delftstack.com › home › howto › python › sort 2d array python
How to Sort 2D Array in Python | Delft Stack
February 2, 2024 - from operator import itemgetter lst = [ ("john", "C", 15), ("jane", "A", 12), ("dave", "D", 10), ] sorted_lst = sorted(lst, key=itemgetter(1)) print(sorted_lst) ... Enjoying our tutorials?
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › how to sort by the second element in a 2d list
r/learnpython on Reddit: How to sort by the second element in a 2D list
February 20, 2021 - The sort function (of course) needs values to compare, so when it scans through the list, the values it “sees” correspond to L[1] for each sublist L, i.e. the numbers in your 2D list. As you noted, without that key function the list-to-list comparison is essentially just a lexicographical sort by the first element, unless there’s a tie, in which case it looks at the number. ... Starting learning Python at 50.
🌐
Reddit
reddit.com › r/learnpython › sorting 2d array
r/learnpython on Reddit: Sorting 2d Array
June 28, 2023 - def unpack_list(l: list, t_lst: list): [l.extend(x) for x in t_lst] l.sort() b_list = [[1, 6], [4, 3], [5, 2]] lst = [] unpack_list(lst, b_list) print([[x, y] for x, y in (zip(lst[::2], lst[1::2]))]) ... list_a = [[1, 6], [4,3], [5,2]] sorted_1d = sorted(sum(list_a, [])) # or from itertools import chain sorted_1d = sorted(chain.from_iterable(list_a)) ... array_2d = [[1, 6], [4, 3], [5, 2]] sorted_array = sorted(array_2d, key=lambda x: x[0]) print(sorted_array)
🌐
Mrgoff
mrgoff.com › py_lists
Python toolbox MrGoff.com
The first command here would clear the sub-list in position 1 i.e. the second sub-list. This would leave the scores list as [[15,19,22],[],[7,12,9],[15,18,12]]. The second clear command would leave an empty one-dimensional list. Lambda sorting can allow us to sort a 2D list based on the value in a certain position in each sub-list.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sort-list-according-second-element-sublist
Sort a list according to the second element of sublist - Python - GeeksforGeeks
May 9, 2025 - Explanation: sorted(a, key=lambda x: x[1]) sorts the list a by the second element of each sublist, using a lambda function to specify that the sorting should be done based on the value at index 1 of each sublist.
🌐
University of Kentucky
cs.uky.edu › ~keen › 115 › lectures › sort-by-any-column.html
Untitled
from random import randint # Demonstrates sorting 2-dimensional lists by any column # the get_item function should return the element in the desired column # the only purpose of this function is to return the 2nd element in the # 1-dimensional list that is the parameter def get_item(lst): return lst[1] def main(): # building a list of 8 rows which have 2 random numbers each mylst = [] for i in range(8): mylst.append( [randint(10,50), randint(100, 200)] ) print("Original list", mylst) print("\nSorted by first column, ascending order", sorted(mylst) ) # sorted by first column print("\nSorted by first column, descending order", sorted(mylst, reverse = True)) print("\n\nSorted by second column, ascending order", sorted(mylst, key=get_item)) print("\nSorted by second column, descending order", sorted(mylst, key=get_item, reverse = True)) print("\nOriginal list unchanged", mylst) main()
🌐
Stack Overflow
stackoverflow.com › questions › 62395588 › sort-2d-list-in-ascending-order
python - Sort 2D list in ascending order - Stack Overflow
Just like that your list is sorted. It can even sort you list which contains sublist. Just change the variable MyList to your own list. Now what if we wanted to reverse it, that's simple just set reverse to true · MyList = [13,7,4,4] sortedList = sorted(MyList,reverse=True)
🌐
Python Forum
python-forum.io › thread-21310.html
sorting 2D lists by column
September 24, 2019 - Hello everyone, I would like to sort a multidimensional list by the second column. Example: x = [ [1,18,2], [2,9,4], [3,1,1] ] x = sorted(x, key=lambda x: x[2]) print(x)The output I am looking for should be: [ [3,1,1], [2,9,4], [1,...
🌐
Stack Overflow
stackoverflow.com › questions › 27115968 › sorting-on-a-2d-list
python - Sorting on a 2D list - Stack Overflow
I have a 2D list that I have created from reading in a txt file. I have written this code to sort the list by the highest value - position 5. sort = list(sorted(sort,key=lambda x: x[5],reverse=Tr...