Monday, May 20, 2013

Sorting a List of Lists in Python



ListOfLists[0] = ListList[0] = bob; List[1] = otherstring; List[2] = 3;for list in ListOfLists:    tmp_toup = (int(list[2]),list)      
    ProcessTuple.append(tmp_toup)    #this sorts my tuple_list in order with the list[2] as a key                      
    sorted_tuple_list = sorted(ProcessTuple , key=lambda x: x[0])

This snippit is very useful for sorting a list of lists. One could also use a map for this implementation but if one wants to keep duplicates this is a good option. I have used this snippet many times.

No comments:

Post a Comment