Home > Enterprise >  How do i sort my list of tuples in ascending order e.g. my_list = [(6,4), (3,4)] to produce (3,4) (4
How do i sort my list of tuples in ascending order e.g. my_list = [(6,4), (3,4)] to produce (3,4) (4

Time:11-21

How can i sort the below list of tuples to produce tuples of (3,4) (4,6)

my_list = [(6,4), (3,4)]

I have tried the following

items= [(3,4),(6,4)]
sorted_items= sorted(items)
print(sorted_items)

and

my_list = [(6,4), (3,4)]
my_list.sort(key=lambda tup: (tup[0], tup[1]), reverse=False)
print(my_list)

Thanks

CodePudding user response:

You can use two calls to sorted() to generate the desired output:

sorted(tuple(sorted(tup)) for tup in my_list)

This outputs:

[(3, 4), (4, 6)]
  • Related