list comprehension

Here are a few examples for list comprehensions:

#
# Print a list. List comprehension works only with
# expressions. Since {\tt print} is a statement, we have
# to import the print-function from __future__:
#
#!/usr/bin/env python
from __future__ import print_function

lst = [ 'c', 'b', 'a']

[ print( elm) for elm in sorted( lst)]

#
# create a list
#
In [1]: lst = [n for n in range(10)]

#
# create a list using a contraint
#
In [3]: lst = [n for n in range(10) if n > 2]

#
# create a list of tuples
#
In [5]: lst = [(n,n+1) for n in range(10) if n > 2]

#
# create a dictionary
#
In [12]: lst1 = [0, 1, 2, 3]
In [13]: lst2 = ['a', 'b', 'c', 'd']
In [14]: hsh = { k: lst2[k] for k in lst1}
#
# check, if position is in the list of attributes
#
'position' in list( map( lambda x: x.lower(), proxy.get_attribute_list()))
'position' in list( map( str.lower, proxy.get_attribute_list()))

An example of how to create an iterator using list comprehension can be found in section 15.6.1