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
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
python - sort a 2D list first by 1st column and then by 2nd column - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I am trying to find a nice way to sort a 2d list , first by the 1st value , and then by the 2nd value. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
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 ...
๐ŸŒ
Script Everything
scripteverything.com โ€บ posts โ€บ 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 | Script Everything
July 29, 2021 - To sort a two-dimensional list using multiple columns and different sorting methods (eg. descending order for one, ascending order for another) in Python, without using any imported libraries, use the built-in sort() list method and sorted() function. By using the built-in sort list method ...
๐ŸŒ
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 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

๐ŸŒ
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.
๐ŸŒ
HatchJS
hatchjs.com โ€บ home โ€บ how to sort a 2d list in python
How to Sort a 2D List in Python
January 5, 2024 - A: To sort a 2D list by multiple columns in Python, you can use the `sort()` method with the `key` argument. The `key` argument specifies a function that will be used to extract a value from each element of the list.
๐ŸŒ
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)
๐ŸŒ
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()
๐ŸŒ
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...