I store data in dictionary, where key is an integer, and value is a tuple of integers.
I need to get the length of the longest element, and its key.
I found this for getting the max value over dict:
def GetMaxFlow(flows):
maks=max(flows, key=flows.get)
return flows[maks],maks
I tried to modify and as a key use the len function, but it didn’t work, so I tried something reasonable and straightforward, yet inefficient:
def GetMaxFlow(flows):
Lens={}
for a in flows.iteritems():
Lens[a[0]]=len(a[1])
maks=max(Lens, key=Lens.get)
return Lens[maks],maks
Is there a more elegant, and pythonic way to do it?
This is one of the reasons
lambdastill exists in Python I think.To specifically return a len…
Or use eumiro‘s solution, which actually makes more sense in this case. (I misunderstood your question.)