Python Forum
Sort List of Lists by Column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sort List of Lists by Column
Hello all,
I am working on a small Python project and need some help.
I have a CSV file that is structured as follows:

Nr, Name, weight , height, with
112,Aerified, 41.70, 256.99, 141.01
656, Zoolab, 66.64, 108.44, 269.53

There are 10.000 randomly generated rows und the file.

Now I want to sort this file according to a selected column and output it.
For this I don't want to use methods but solve it with an algorithm.
For this I first imported it with pandas and then converted it to a list (hoping to be able to process it better):

frame = pd.concat(li, axis=0, ignore_index=True)
products_list = frame.values.tolist()
That has also worked so far.
I have already tried it with a bubble sort:
def sort(product_list):

  for passnum in range(len(product_list)-1, 0, -1):
     for i in range(passnum):
        if int(product_list[i][2]) > int(product_list[i+1][2]):
            temp = product_list[i]
            product_list[i] = product_list[i+1]
            product_list[i+1] = temp
  return product_list
The I wanted to sort in this case after the 3rd column . But unfortunately it does not work.
Do you have any other ideas?
Your first snippet doesn't define li, so I'm not completely sure of what format you have.

Without that or other information, we don't know what "does not work" means. Does it error, does the sort seem wrong? Please give inputs that show what is wrong. When I tried it, it seemed to work. The given data is already sorted on the third column.

That said:
  • You don't need temp variables to swap items. You can instead assign them at the same time like:
    product_list[i], product_list[i+1] = product_list[i+1], product_list[i]
  • Is there a reason you are converting the numbers to int()? As they have decimals, I could see float() if you were converting from text. But otherwise you are losing precision.

Possibly Related Threads…
Thread Author Replies Views Last Post
Photo a.sort() == b.sort() all the time 3lnyn0 1 456 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  list sort() function bring backs None CompleteNewb 6 991 Mar-26-2022, 03:34 AM
Last Post: Larz60+
  How to efficiently average same entries of lists in a list xquad 5 1,028 Dec-17-2021, 04:44 PM
Last Post: xquad
  Not able to add extra column to the list in the python shantanu97 2 884 Nov-17-2021, 10:14 AM
Last Post: snippsat
  sorting a list of lists by an element leapcfm 3 990 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  [solved] Sort list paul18fr 5 1,680 Aug-18-2021, 06:34 AM
Last Post: naughtyCat
  behavior list of lists roym 5 1,118 Jul-04-2021, 04:43 PM
Last Post: roym
  List of lists - merge sublists with common elements medatib531 1 2,316 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Space between list and column alignment rturus 8 2,871 Mar-17-2021, 04:47 PM
Last Post: rturus
  Creating list of lists from generator object t4keheart 1 1,457 Nov-13-2020, 04:59 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020