C5W2A2 - Minor Python clarification requested

This is really a Python not a course question, but when we use max() to find the longest sentence (maxLen), I can understand why we use .split() once-- But I’m not sure I understand what is going on the second time (or why twice) ?

That said, I am also still trying to wrap my head around Lambda functions so sometimes I still struggle a little with those…

Last split is because max function returns the longest string, not the size of string. So at the end, you need to split that longest string again, and calculate the length of it. Here it follows an example so you can practice on your python environment.

X_train = [
“First message.”,
“Second message, a little bit larger.”,
“Third and last message.”,
]

maxLen = len(max(X_train, key=lambda x: len(x.split())).split())

print (maxLen)

1 Like

Yeah. I will try to go over it piece by piece:

max(X_train, key=lambda x: len(x.split())).split():X_train is a list of strings.

x.split() splits each string x in the list X_train into a list of words. The default behavior of the split() method is to split by whitespace.

len(x.split()) calculates the number of words in each string

x.key=lambda x: len(x.split()) is a function that the max() function uses to determine the criteria for “maximality.” Here, it means that the max() function will compare the lengths (number of words) of each string rather than the strings themselves.

Lambda functions like these are also used in sorted. eg while using sorted, you add the key as a lambda to determine the criteria for sorting.

max(X_train, key=lambda x: len(x.split())) returns the string from X_train that has the maximum number of words. .split() is applied to the string returned by the previous step, turning it into a list of words.

len(...): This wraps the previous split() method to count the number of words in the longest string (the one with the maximum words).

1 Like