Wednesday, March 15, 2017

Dictionary in Python

Find the value with the maximum length in a dictionary:

def biggest(aDict):
    '''
    aDict: A dictionary, where all the values are lists.

    returns: The key with the largest number of values associated with it
    '''
    # Your Code Here
    result = None
    maximum = 0
    for key in aDict.keys():
        if len(aDict[key]) >= maximum:
            result=key
            maximum = len(aDict[key])
    return result

Returns the sum of the number of values associated with a dictionary

def how_many(aDict):
    '''
    aDict: A dictionary, where all the values are lists.

    returns: int, how many values are in the dictionary.
    '''
    # Your Code Here
    result=0
    for key in aDict.keys():
        result+=len(aDict[key])
    return result

No comments:

Post a Comment