Original Translation
40
Unicode-aware regular expressions are available through the :mod:`re` module, which has a new underlying implementation called SRE written by Fredrik Lundh of Secret Labs AB.
41
A ``-U`` command line option was added which causes the Python compiler to interpret all string literals as Unicode string literals. This is intended to be used in testing and future-proofing your Python code, since some future version of Python may drop support for 8-bit strings and provide only Unicode strings.
42
List Comprehensions
43
Lists are a workhorse data type in Python, and many programs manipulate a list at some point. Two common operations on lists are to loop over them, and either pick out the elements that meet a certain criterion, or apply some function to each element. For example, given a list of strings, you might want to pull out all the strings containing a given substring, or strip off trailing whitespace from each line.
44
The existing :func:`map` and :func:`filter` functions can be used for this purpose, but they require a function as one of their arguments. This is fine if there's an existing built-in function that can be passed directly, but if there isn't, you have to create a little function to do the required work, and Python's scoping rules make the result ugly if the little function needs additional information. Take the first example in the previous paragraph, finding all the strings in the list containing a given substring. You could write the following to do it::
45
# Given the list L, make a list of all strings # containing the substring S. sublist = filter( lambda s, substring=S: string.find(s, substring) != -1, L)
46
Because of Python's scoping rules, a default argument is used so that the anonymous function created by the :keyword:`lambda` statement knows what substring is being searched for. List comprehensions make this cleaner::
47
sublist = [ s for s in L if string.find(s, S) != -1 ]
48
List comprehensions have the form::
49
[ expression for expr in sequence1 for expr2 in sequence2 ... for exprN in sequenceN if condition ]