For example, I have a list:
[[1,3],[23,4],[13,45,6],[8,3],[44,33,12]]
Is there any efficient way that I can finally get the list below?
[[1,3],[13,45,6]]
For every length of the list, keep only one element.
Just make a dictionary keyed off the length and get its values:
>>> l = [[1,3],[23,4],[13,45,6],[8,3],[44,33,12]]
>>> dict((len(i), i) for i in l).values()
[[8, 3], [44, 33, 12]]
s = set()
[e for e in l if len(e) not in s and s.add(len(e)) is None]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With