2
Contributors
1
Reply
9
Views
11 Years
Discussion Span
Last Post by vegaseat
2

Python's lambda allows you to declare a one-line nameless minifunction on the fly. The format is:
lambda parameter(s): expression using the parameter(s)
It returns the result of the expression. The expression has to be a one-liner (no newlines)! Here is a little example ...

# sort strings in a list, case insensitive
import string

wordList = ['Python', 'is', 'really', 'great', 'stuff']
print "Original list:"
print wordList

wordList.sort()
print "After standard sort (ASCII upper case is before lower case):"
print wordList

wordList.sort(lambda x, y: cmp(string.lower(x), string.lower(y)))
print "After case insensitve sort with lambda and lower:"
print wordList

I decided to put that also under "Starting Python".

Edited by vegaseat: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.