Vinci Vinci - 1 month ago 6
Python Question

How to print min of set in python?

I have a set

ww = [{27, 29, 6, 71}, {75, 52, 29, 46, 7}, {20, 71}, {59}, {52, 37, 15}]


and I want to print the element that has the shortest length ie. {59} in this example.

min(ww)


would only give me {27, 29, 6, 71} instead of {59}

is there a way of using min() to do this? or any other smart methods?

Answer

Try this,

In [3]: min(ww,key=len)
Out[3]: {59}

Or

In [4]: sorted(ww,key=len)[0]
Out[4]: {59}
Comments