Python sort method key parameter


In the myFunc function below, where is the (e) argument passed in from? (I hope I’m using the right terminology)

# A function that returns the length of the value:
def myFunc(e):
  return len(e)

cars = ['Ford', 'Mitsubishi', 'BMW', 'VW']

Asked By: JR Unisa



the function myFunc is called by cars.sort(key=myFunc) and each item in the list passed as an argument to myFunc. parameter name is not required here as it can work with the positional parameter. but we still need to have one parameter myFunc so that it can receive the passed value.

also the code can be simplified like below by using len() method directly instead of wrapping it in myFunc.


Hope this helps.

Answered By: Eswar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.