Understanding max function function in Python dictionary

I'm trying to understand the max operation of a function in a Python dictionary. Below is the code I am using:

tall_buildings = { 
    "Empire State": 381, "Sears Tower": 442,
    "Burj Khalifa": 828, "Taipei 101": 509 
}


# 1. find the height of the tallest building
print("Height of the tallest building: ", max(tall_buildings.values()))


# 2. find the name, height pair that is tallest
print(max(tall_buildings.items(), key=lambda b: b[1]))


# 3. find the tallest building
print(max(tall_buildings, key=tall_buildings.get))

      

All of the above printing instructions give correct results as noted in the comments in the code.

I figured out how #1

and #2

.

1: tall_buildings.values()

gives the flow of heights and the function max

returns the maximum height.

2: tall_buildings.items()

gives a stream of pairs (name, height) and the max function returns a pair based onkey=pair height.

However, I am having a hard time understanding how it works # 3

. What key=tall_buildings.get

is the key to finding the tallest building?

I took the code from Ned Pycon Talk: https://youtu.be/EnSu9hHGq5o?t=12m42s

+3


source to share


3 answers


The max()

function iterates over the first argument, applies the key function to each item, and selects the item with the maximum key.

Iterating over a dictionary is the same as iterating over its keys. By doing



max(tall_buildings, key=tall_buildings.get)

      

we will first go through all the keys in tall_buildings

. For each key, a k

key function will be evaluated tall_buildings.get(k)

, which returns the height of the building indicated by k

. It will then be selected and returned k

with the maximum height.

+1


source


How # 3 works, the method provided as key

just looks up the value from the dictionary tall_buildings

. Thus, for each key

repeating one, a corresponding one value

will be provided get

.

Method get

is synonymous with operator[]

>>> tall_buildings['Sears Tower']
442
>>> tall_buildings.get('Sears Tower')
442

      

Reason # 3 is for looping over the keys in the first place, is that by default iterating over dict

will only iterate over the keys



for i in tall_buildings:
    print(i)

Taipei 101
Empire State
Burj Khalifa
Sears Tower

      

You can also explicitly loop through the keys

for i in tall_buildings.keys():
    print(i)

Taipei 101
Empire State
Burj Khalifa
Sears Tower

      

Similarly, you can iterate over .values()

which are only the values ​​in the dictionary, or .items()

that iterate over tuples of pairs (key,value)

.

+2


source


The max concept requires a definition of the ordering of items.

So, here you provide a parameter key

just as you would do with sort : a function that is applied to each element of the dictionary to match pairs (key, val)

to values ​​that have an inline ordering definition (e.g. numbers, strings). This way you will find the maximum value of the displayed values ​​and the result will be the corresponding element in the original dictionary.

+1


source







All Articles