Find set with smallest number of elements from a list of sets [duplicate]












2















This question already has an answer here:




  • How does the min/max function on a nested list work?

    4 answers




I have a list of sets -



inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]



I want -



{4, 5} (set with smallest number of elements)



My code -



length = float("inf")
small = {}
for x in inconsistent_case:
if len(x) < length:
length = len(x)
small = x
print(small)


Which gives me -



{4, 5}



Is there any fastest and/or easiest way to do this?










share|improve this question













marked as duplicate by Patrick Haugh, Noctis Skytower, Community Nov 13 '18 at 6:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Thanks @Patrick Haugh, marked it as duplicate.
    – Ruturaj
    Nov 13 '18 at 6:21
















2















This question already has an answer here:




  • How does the min/max function on a nested list work?

    4 answers




I have a list of sets -



inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]



I want -



{4, 5} (set with smallest number of elements)



My code -



length = float("inf")
small = {}
for x in inconsistent_case:
if len(x) < length:
length = len(x)
small = x
print(small)


Which gives me -



{4, 5}



Is there any fastest and/or easiest way to do this?










share|improve this question













marked as duplicate by Patrick Haugh, Noctis Skytower, Community Nov 13 '18 at 6:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Thanks @Patrick Haugh, marked it as duplicate.
    – Ruturaj
    Nov 13 '18 at 6:21














2












2








2








This question already has an answer here:




  • How does the min/max function on a nested list work?

    4 answers




I have a list of sets -



inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]



I want -



{4, 5} (set with smallest number of elements)



My code -



length = float("inf")
small = {}
for x in inconsistent_case:
if len(x) < length:
length = len(x)
small = x
print(small)


Which gives me -



{4, 5}



Is there any fastest and/or easiest way to do this?










share|improve this question














This question already has an answer here:




  • How does the min/max function on a nested list work?

    4 answers




I have a list of sets -



inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]



I want -



{4, 5} (set with smallest number of elements)



My code -



length = float("inf")
small = {}
for x in inconsistent_case:
if len(x) < length:
length = len(x)
small = x
print(small)


Which gives me -



{4, 5}



Is there any fastest and/or easiest way to do this?





This question already has an answer here:




  • How does the min/max function on a nested list work?

    4 answers








python-3.x nested






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 3:15









Ruturaj

5619




5619




marked as duplicate by Patrick Haugh, Noctis Skytower, Community Nov 13 '18 at 6:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Patrick Haugh, Noctis Skytower, Community Nov 13 '18 at 6:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Thanks @Patrick Haugh, marked it as duplicate.
    – Ruturaj
    Nov 13 '18 at 6:21


















  • Thanks @Patrick Haugh, marked it as duplicate.
    – Ruturaj
    Nov 13 '18 at 6:21
















Thanks @Patrick Haugh, marked it as duplicate.
– Ruturaj
Nov 13 '18 at 6:21




Thanks @Patrick Haugh, marked it as duplicate.
– Ruturaj
Nov 13 '18 at 6:21












1 Answer
1






active

oldest

votes


















3














Yes, specify the key for min:



>>> inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]
>>> min(inconsistent_case, key=len)
{4, 5}


If multiple items are minimal, the function returns the first one encountered.






share|improve this answer





















  • Thanks @wim that works perfectly fine!
    – Ruturaj
    Nov 13 '18 at 3:20


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Yes, specify the key for min:



>>> inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]
>>> min(inconsistent_case, key=len)
{4, 5}


If multiple items are minimal, the function returns the first one encountered.






share|improve this answer





















  • Thanks @wim that works perfectly fine!
    – Ruturaj
    Nov 13 '18 at 3:20
















3














Yes, specify the key for min:



>>> inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]
>>> min(inconsistent_case, key=len)
{4, 5}


If multiple items are minimal, the function returns the first one encountered.






share|improve this answer





















  • Thanks @wim that works perfectly fine!
    – Ruturaj
    Nov 13 '18 at 3:20














3












3








3






Yes, specify the key for min:



>>> inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]
>>> min(inconsistent_case, key=len)
{4, 5}


If multiple items are minimal, the function returns the first one encountered.






share|improve this answer












Yes, specify the key for min:



>>> inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]
>>> min(inconsistent_case, key=len)
{4, 5}


If multiple items are minimal, the function returns the first one encountered.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 3:18









wim

158k50300430




158k50300430












  • Thanks @wim that works perfectly fine!
    – Ruturaj
    Nov 13 '18 at 3:20


















  • Thanks @wim that works perfectly fine!
    – Ruturaj
    Nov 13 '18 at 3:20
















Thanks @wim that works perfectly fine!
– Ruturaj
Nov 13 '18 at 3:20




Thanks @wim that works perfectly fine!
– Ruturaj
Nov 13 '18 at 3:20



Popular posts from this blog

Bressuire

Vorschmack

Quarantine