Python: lambda in a dictionary


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // add a function as an element of a dictionary
  2. >>> {1: lambda: len('ciao')}
  3. >>> {1: <function <lambda> at 0x101d530>}
  4. // get element with specified key
  5. >>> {1: lambda: len('ciao')}[1]
  6. >>> <function <lambda> at 0x101d5f0>
  7.  
  8. >>> a = {1: lambda: len('ciao')}[1]
  9. >>> a
  10. >>> <function <lambda> at 0x101d670>
  11. // run the function-element
  12. >>> a()
  13. >>> 4
  14.  
  15. // or run it directly from here!
  16. >>> {1: lambda: len('ciao')}[1]()
  17. >>> 4

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.