Skip to content Skip to sidebar Skip to footer

Sort Nested List: Exclude First Item From Sorting

SET I have a csv file which includes the current balance of my refreshments (it is comma separated, but in this example commas , are removed for improved readability): NAME

Solution 1:

A clean one liner will be:

items_list[1:] = sorted(items_list[1:])

Refer: https://stackoverflow.com/a/5827649/937153

Solution 2:

The easiest way I can think of is to use pandas.

Read the csv using pandas:

df = pd.read_csv("my_refresments.csv")
df.sort_values("PRICE")

which sorts the data according to price, and the header is stored in df.columns, so it is not sorted with the actual data.

Solution 3:

It is simple possible solution,

[items_list[0]] + sorted(items_list[1:])

or Use pandas If there's no reason not to use

pandas sort


thanks to juanpa.arrivillaga

To explain why use sorted() not .sort()

sorted() return sorted list, but .sort() is change list it self. (not return)

so If you want to use .sort() then code will be

values = items_list[1:].sort()
item_list = [item_list[0]] + values

using sorted is more short!

Post a Comment for "Sort Nested List: Exclude First Item From Sorting"