Determining Key with Most Values

Melanie
dictionary = {"key1": ["Item1", "Item2"], "key2": ["Item3", "Item4"]}

Working with the above dictionary, trying to iterate through it and return the key with most values.

I was trying this:

def most_values(a):
    return max(a, key=a.get)

Which isn't bad though it's going to return whatever key it checks first. Next I tried:

def most_values(a):
    count = 0
    high = ""
    for t in a:
        if len(a[t]) > count:
            count += 1
            high = t
    return high

But it does the same and will return whatever key it iterated through first. It's also not a very elegant looking solution.

What's the most Pythonic way of going about this?

Willem Van Onsem

The problem with:

return max(a, key=a.get)

is that here the key will return the actual list and in Python lists are compared lexicographically, so not by length (there are things to say for both ways to compare lists, but they decided to sort lexicographically). You can however easily modify this with:

def most_values(a):
    return max(a, key=lambda x:len(a[x]))

This is probably the most Pythonic way since it is declarative (you do not have to think how the maximum is calculated), elegantly, readable and has no side effects.


The problem with your second approach is that you should set count to the new len(a[t]), not increment it. So you can fix it like:

def most_values(a):
    count = -1
    high = None
    for key,val in a.items():
        if len(val) > count:
            count = len(val) # semantical error in your code
            high = key
    return high

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Determining if Swift dictionary contains key and obtaining any of its values

From Dev

Finding key(s) with most values in dictionary

From Dev

Most efficient method determining if a list of values completely satisfy a one to many relationship (MySQL)

From Dev

Determining Super Key

From Dev

Determining Super Key

From Dev

Determining values in a bitmask

From Dev

Determining the values of Boolean variables

From Dev

How to return the key with the most associated values inside a HashMap

From Dev

Determining portably the most restrictive data type for alignment

From Dev

Determining the most relevant keyword in a body of text

From Dev

Weighting object values and determining a winner

From Dev

MYSQL - Determining previous values in a row

From Dev

Adding bytes and determining flag values

From Dev

Sort an excel sheet by determining the key column dynamically

From Dev

Fastest way of determining most frequent factor in a grouped data frame in dplyr

From Dev

Determining which screen a window is on (by checking where the most surfacearea is located)

From Dev

Determining the element that occurred the most in O(n) time and O(1) space

From Dev

Determining if values of previous rows repeat in dataframe

From Dev

Determining the range of values returned by Python's hash()

From Java

Determining if all values in a list of lists are true or false

From Dev

Determining whether an array contains duplicate values

From Dev

Determining median values with nested IF and AND array formulas

From Dev

Determining duplicate values before a form is submitted

From Dev

Efficient algorithm for determining values not as frequent in a list

From Dev

Determining ratios to reach average of a quantity of 3 values

From Dev

Determining if a variable is from a list of several values in Lua

From Dev

BIRT Designer: Determining Percentage of Total for Values in a Column

From Dev

Determining whether values held in variables are unique

From Dev

Determining averages, stdev, stderror, and counts of values in a list

Related Related

  1. 1

    Determining if Swift dictionary contains key and obtaining any of its values

  2. 2

    Finding key(s) with most values in dictionary

  3. 3

    Most efficient method determining if a list of values completely satisfy a one to many relationship (MySQL)

  4. 4

    Determining Super Key

  5. 5

    Determining Super Key

  6. 6

    Determining values in a bitmask

  7. 7

    Determining the values of Boolean variables

  8. 8

    How to return the key with the most associated values inside a HashMap

  9. 9

    Determining portably the most restrictive data type for alignment

  10. 10

    Determining the most relevant keyword in a body of text

  11. 11

    Weighting object values and determining a winner

  12. 12

    MYSQL - Determining previous values in a row

  13. 13

    Adding bytes and determining flag values

  14. 14

    Sort an excel sheet by determining the key column dynamically

  15. 15

    Fastest way of determining most frequent factor in a grouped data frame in dplyr

  16. 16

    Determining which screen a window is on (by checking where the most surfacearea is located)

  17. 17

    Determining the element that occurred the most in O(n) time and O(1) space

  18. 18

    Determining if values of previous rows repeat in dataframe

  19. 19

    Determining the range of values returned by Python's hash()

  20. 20

    Determining if all values in a list of lists are true or false

  21. 21

    Determining whether an array contains duplicate values

  22. 22

    Determining median values with nested IF and AND array formulas

  23. 23

    Determining duplicate values before a form is submitted

  24. 24

    Efficient algorithm for determining values not as frequent in a list

  25. 25

    Determining ratios to reach average of a quantity of 3 values

  26. 26

    Determining if a variable is from a list of several values in Lua

  27. 27

    BIRT Designer: Determining Percentage of Total for Values in a Column

  28. 28

    Determining whether values held in variables are unique

  29. 29

    Determining averages, stdev, stderror, and counts of values in a list

HotTag

Archive