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
  List of lists - merge sublists with common elements medatib531 1 350 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Space between list and column alignment rturus 8 1,055 Mar-17-2021, 04:47 PM
Last Post: rturus
  Creating list of lists from generator object t4keheart 1 496 Nov-13-2020, 04:59 AM
Last Post: perfringo
  How do i make a new lists out of an list ozezn1 1 422 Oct-28-2020, 10:19 PM
Last Post: Gribouillis
  How to sort os.walk list? Denial 6 1,224 Oct-10-2020, 05:28 AM
Last Post: Denial
  List index out of range error while accessing 2 lists in python K11 2 588 Sep-29-2020, 05:24 AM
Last Post: K11
  list indices must be integers or slices, not lists error djwilson0495 2 764 Aug-27-2020, 06:13 PM
Last Post: deanhystad
  Get Value from List to Show in DataFrame Column ahmedwaqas92 1 664 Jun-22-2020, 08:24 AM
Last Post: ahmedwaqas92
  How to put the items of one list in new generated lists Bobbear 1 565 Jun-12-2020, 06:08 AM
Last Post: buran
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 1,079 May-10-2020, 05:28 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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