python max with custom function 1

python max with custom function

nums = [1,2,3,1,1,3,3,4,4,3,5] #list
counts = collections.Counter(nums) # create a dict of counts
#using counts.get() fun as custom function 
return max(counts.keys(), key=counts.get)  

Here is what the above code is Doing:
1. counts.keys() returns a list of all the unique keys in the counts dictionary.
2. counts.get() is a function that takes a key as input and returns the value associated with that key.
3. max() is a function that takes a list and a custom function as input and returns the element of the list that maximizes the custom function.

Similar Posts