How to determine who visited the White House the most each month?

I’m struggling to figure out how to find the most visited person each month. The code below is taken initially from here with some tweaks:

# Function to extract maximum visits of an individual
def max_person(data):
    person = {}
    for row in data:
        name = row[0]
        person[name] = person.get(name, 0) + 1
    return [max(person, key = person.get), person[max(person, key = person.get)]] 
# Store months from "potus" data set
months = []
for row in potus:
    month_name = row[2].strftime('%b')
    if month_name not in months:
# Finding the most visited person in each month
for month in months:
    visitors_list = []
    for row in potus:
        month_name = row[2].strftime('%b')
        if month_name == month:
    print([month] + max_person(visitors_list))


['Jan', 'Jesus MurilloKaram', 3]
['Feb', 'Sheila JacksonLee', 5]
['Mar', 'Anna M. Yeo', 3]
['Apr', 'Alan C. Prather', 7]
['May', 'Chris Coons', 6]
['Jun', 'Marilda W. Averbug', 6]
['Jul', 'Russell A. Wilson', 4]
['Aug', 'ELIZABETH C. NUNEZ', 3]
['Sep', 'Shaojun ■■■■', 6]
['Oct', 'AnnaMaria R. Mottola', 8]
['Dec', 'Brian E. Fallon', 4]

I’m not satisfied with this approach. I believe this can be done with nested dictionaries but I have been struggling to build the logic since yesterday. I am looking for something like:

{'Jan': {'Jesus MurilloKaram': 3},
'Feb': {'Sheila JacksonLee': 5},
'Mar': {'Anna M. Yeo': 3},
'Dec': {'Brian E. Fallon': 4}}

I am open to any idea to improve my solution. Thank you all for your consideration.

You just need to change code in this block.

  1. Create an empty dictionary before the loop.
  2. Replace the print statement with an appropriate dictionary assignment.
  3. Print the dictionary after the loop.

Thank you for guiding me, here’s my output:

{'Jan': ['Jesus MurilloKaram', 3],
 'Feb': ['Sheila JacksonLee', 5],
 'Mar': ['Anna M. Yeo', 3],
 'Apr': ['Alan C. Prather', 7],
 'May': ['Chris Coons', 6],
 'Jun': ['Marilda W. Averbug', 6],
 'Jul': ['Russell A. Wilson', 4],
 'Aug': ['ELIZABETH C. NUNEZ', 3],
 'Sep': ['Shaojun ■■■■', 6],
 'Oct': ['AnnaMaria R. Mottola', 8],
 'Dec': ['Brian E. Fallon', 4]}

And this is what I was looking for.
You’re awesome!!

1 Like