Hi all,

I would like to ask. Im Fairuz and the new member for this forum. I having problems to sort the multi dimentional array in python. ie:
[('DcaUiVb_B09398-11', [None]), ('m3sui_R66441-35', []), ('m3sui_r26557-01', []), ('m3sui_R31225-24', []), ('m3sui_r66147-03', []), ('m3sui_R31225-23', []), ('m3sui_R30803-10', [])]

I have the list but im having problems to sort it according to the first word ie: ('m3sui_r66147-03', []). Im new in python code. Is there anyone that can guide me to do the code for this problems?

Thanks in advance.

7 Years
Discussion Span
Last Post by Gribouillis

Yes l is the list. For the sort method lookup the definition for list.sort.
lambda x:x[0] is equivalent to
def f(x): return x[0]


i have this list:

Component List: [('DcaUiVb_B09398-10', 0), ('DcaUiVb_B09398-11', 5), ('m3sui_R66441-35', 0), ('fairuz', 0), ('shukor', 6)]

I would like to sort it based on the second value (which is in RED) in reverse order. i tried to do List.sort() but it didnt works.

anyone can give me idea? Thanks!


Define a score function for the list items, and sort the list according to this score function

# python 2 and 3
def score(item):
    return int(item[1])

mylist = [('DcaUiVb_B09398-10', 0), ('DcaUiVb_B09398-11', 5), ('m3sui_R66441-35', 0), ('fairuz', 0), ('shukor', 6)]
print(sorted(mylist, key=score, reverse=True))

Edited by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.