def aggregate_by_player_id(statistics, playerid, fields):
    """
    Inputs:
      statistics - List of batting statistics dictionaries
      playerid   - Player ID field name
      fields     - List of fields to aggregate
    Output:
      Returns a nested dictionary whose keys are player IDs and whose values
      are dictionaries of aggregated stats.  Only the fields from the fields
      input will be aggregated in the aggregated stats dictionaries.
    """

    dictt = {}

    for dit in statistics:
        dictt[dit[playerid]] = {}
        for field in fields:
            dictt[dit[playerid]][field] = 0
            dictt[dit[playerid]][playerid] = dit[playerid]

    # Everything is set up, only addition of fields is due:
    #print(d)

    for key, val in dictt.items():
        for dit in statistics:
            if key == dit[playerid]:
                # print(True)
                for stat in fields:
                    dictt[key][stat] += int(dit[stat])
    return dictt