I know that lambda is some kind of function in Python. What does it do? Why would one use it?

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

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".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.