0

here is the list that I want to merge

l = [3.23, 5.32, 8.23, 2.23, 9.98, 7.43, 6.43, 8.23, 4.23]

the list has to be sorted from high to low

l.sort()
l.reverse

I need this as a result

p = [(9.98, 8.23, 8.23, 7.43), (6.43, 5.32, 4.23, 3.23), (2.23,)]

Can someone help me do this in python?

JoshL1516
  • 117
  • 7

1 Answers1

1

First sort the list in descendant order:

l=sorted(l,reverse=True)

Then use list comprehension to merge the list into tuples of 4 items:

[tuple(l[i:i + 4]) for i in range(0, len(l), 4)]
zoldxk
  • 2,632
  • 1
  • 7
  • 29