0

I have this dictionary

BRANDS = {
   "Toyota": { "shortname": lambda s: s[:2] },
   "Toyota Asia": { "shortname": lambda s: 'TyA' }
}

When cycling over the brands, I use the supplied lambda to get me a shortname for the brand. However, I would like to do some more complex things in that lambda. Is there a way in Python 3 to provide an anoymous function (like we can do in C# or Javascript)? I know I can create a named function using def, but I want all definition in the dictionary for administrative readons.

Whenever I search for anonymous functions online, I only get the lambda expressions.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • 3
    Nope, lambdas are limited to a single expression. Anything longer needs to be regular `def` function – jonrsharpe Apr 03 '20 at 08:54
  • Within the single expression in a lambda, you do have the option of calling other functions, built-in or not. – quamrana Apr 03 '20 at 08:58

0 Answers0