How to Find the visitor who spent the most amount of time at the White House. 353

Screen Link:

My Code:

for row in potus:
    name = row[0]
    if name not in frequent_guests:
        frequent_guests[name] = 1
        frequent_guests[name] += 1
topguest = max(frequent_guests)  
print (topguest, frequent_guests[topguest])

What I expected to happen:
return the name of the guest that has visited the white house the most

What actually happened:
returned with a guest who visited only once

1 Like

Hello @kitskoiki,

Kindly post the full code. frequent_guests is not defined in the code posted above.

There may be bugs if you copy DQ solution.

Here’s my solution

app_lengths = dict()

for row in potus:

    start_date = row[2]
    end_date = dt.datetime.strptime(row[3], "%m/%d/%y %H:%M")
    length = end_date - start_date 
    appt_lengths[length] = appt_lengths.get(length, 0) + 1

min_length = min(appt_lengths)
max_length = max(appt_lengths)

Note that

  • row[3] is of string type with format as "%m/%d/%y %H:%M"

  • row[2] is of datetime.datetime type.

  • Suppose appt_lengths is empty. Then min(appt_lengths) and max(appt_lengths) will throw an error because an empty sequence.

Sorry! My mistake, forgot to add copy it with the code I pasted here

It’s defined as an empty dictionary in my code.

Thanks but this isn’t my issue. I’ve done this part already.

I was trying to explore the data further to see who had been to the White House the most.

Change to storing name instead of counting the frequency.

max or min will use the length on the value of the dictionary.

Use defaultdict because we don’t need to check if a key exists or not, simply add into the dictionary.

from collections import defaultdict 
appt_lengths = defaultdict(list)

for row in potus:
    start = row[2]
    end = dt.datetime.strptime(row[3], "%m/%d/%y %H:%M")
    length = end - start 
min_length = min(appt_lengths)
max_length = max(appt_lengths)

most_visitor_in_key = max(appt_lengths, key = lambda x: len(appt_lengths[x]))
print("Most visitors in the dictionary key:")

longest_time_delta_in_key = max(appt_lengths) # or max(appt_lengths.keys())
print("Longest time delta in the dictionary key:")


  • max_length = max(appt_lengths) is the maximum key of the dictionary = longest time delta (time differences between start and end.)

  • min_length = min(appt_lengths) is the minimum key of the dictionary = shortest time delta (time differences between start and end.)

  • max(appt_lengths) and max(appt_lengths.keys())is the same.

  • min(appt_lengths) and min(appt_lengths.keys()) is the same.

Hi @kitskoiki, here is another approach to solve this:

Since your code above creates a dictionary frequent_guests and stores the name of the guests as the dictionary keys and the number of times they go to the white house as the dictionary values, you will have to find the dictionary key that has the highest/maximum value.

To find the key with the max value in the dictionary, we use max(dict_name, key = dict_name.get), The key parameter to the max() function is a function that computes a key that is used to determine how to rank items.

Hence the correct line of code should be: topguest = max(frequent_guests, key = frequent_guests.get).

You could also use this line of code to populate your dictionary instead of the for loop: frequent_guests[name] = frequent_guests.get(name, 0) + 1.

Read the full details of the different ways I have used .get here.

Hence, your new code should look like this:

frequent_guests = {}
for row in potus:
    name = row[0]

    frequent_guests[name] = frequent_guests.get(name, 0) + 1
topguest = max(frequent_guests, key=frequent_guests.get)
print (topguest, frequent_guests[topguest])

I hope this answers your question.


yes it does, thanks a lot

1 Like