Sorting tuples in python list

def pairsum(mylist, mytarget):
    liste = []
    for i in mylist:
        for j in mylist:
            if i+j == mytarget:
    lenght = len(liste) ; lenght = int(lenght/2)
    for i in range(lenght):

    return liste
print(pairsum([3,2,6,1,5,4], 7))

I get [(2, 5), (3, 4), (6, 1)] but I want to get the result as [(1,6), (2,5), (3,4)]. What should I do?

You need to sort the mylist first by using the sort() method on the list. Do the sorting before the loops, right after the liste declaration. That’s it!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.