Solution Sharing

Oh yes, and now looking at it I’m not using .values(), so I wouldn’t really need to use max.

for thing in my_dictionary:
  max_value = my_dictionary[thing]
  if my_dictionary[thing] > max_value:
    max_value = my_dictionary[thing]
  if my_dictionary[thing] == max_value:
    return thing

Would it be viable to set this value in the loop, or should it be initialized before the loop?

I think it should be put in the loop right? Because otherwise thing would throw a NameError right?

What if we were to just grab a random value from the dictionary (choose any key and take its value as a starting point)?

for key in my_dictionary:
    max_value = my_dictionary[key]
    max_key = key
    break

Now iterate the object for real…

So you would put this for loop above the other one right? But I thought a variable defined inside a for loop isn’t accessible anywhere but the for loop?

A variable in a function block is scoped to the function (local) but a for loop is not a function. The variables will exist in global scope in this instance.

Yes.

Oh, even if the for loop is inside the function?

The variable will be local, yes, but still available after the loop.

Oh ok but why did you make a max_key? We need the key that has the greatest value right?

Because with each value, we need to keep copy of its key so we can return a final outcome (key associated with the maximum value).

Ok so after having this code,

for key in my_dictionary:
    max_value = my_dictionary[key]
    max_key = key
    break
for thing in my_dictionary:
  if my_dictionary[thing] > max_value:
    max_value = my_dictionary[thing]
  if my_dictionary[thing] == max_value:
    return thing

Now, I’m stuck. How would you use max_key in this for loop?

Let’s use key instead of thing. The key is only the label. The value is the thing (object)

max_value = my_dictionary[key]
max_key = key
for key in my_dictionary:
    max_value = my_dictionary[key]
    max_key = key
    break
for key in my_dictionary:
  if my_dictionary[key] > max_value:
    max_value = my_dictionary[key]
  if my_dictionary[key] == max_value:
    return max_key

Since we don’t know that our initial value was the actual maximum, can we make this inference?

Maybe, but why would we?

if my_dictionary[thing] > max_value:
    max_value = my_dictionary[thing]

This code would only run if there is a value greater than max_value. If not, it would return thing.

But since we don’t know the maximum is it feasible we can return before completely iterating the dictionary?

Wait, I’m kind of lost. In the above for loop, we have a set max_value and a set max_key. In the lower for loop, we iterate through the keys of my_dictionary and check if the current key’s value is greater than max_value. If it is, then we would set max_value equal to that key’s value. Then under that, we check if the current key’s value is equal to max_value and if it is return max_key.

Wait, my code returns the current key if it’s value is equal to max_value, which means that it would stop iterating at a certain point, and that’s not what we want right? We want it to iterate through the entire dictionary.

Right. We need to iterate the entire dictionary and keep a copy of both max_value and max_key as we go.

So maybe I could do something like this:

for key in my_dictionary:
  if my_dictionary[key] > max_value:
    max_value = my_dictionary[key]
    max_key = key

Let that finish iterating all the way, then make another loop.

for key in my_dictionary:
  if my_dictionary[key] == max_value:
    return max_key

So now at least it’s iterating through the entire dict. to get max_value so I can compare them in this next loop?