Python-Ref > Data structures > List > List comprehension
 
 

<-^^
Klíčová slova
Moduly
Knihovní funkce

List comprehension

Quickly and conveniently create lists
Very often one stands before a problem of converting one list of values to another. The items are usually changed in some way and/or sieved in the process. The code below demonstrates the good old way of doing this.
Expand/Shrink
Zdroj: (list9-1.py)
  1   # convert the strings to integers and take only those greater than 50
  2   strings = ["12", "34", "65", "100", "1", "20", "32", "42", "5"]
  3   
  4   result = []              # we create an empty list
  5   for string in strings:
  6       i = int( string)     # we do the conversion
  7       if i > 50:               # those greater than 50...
  8           result.append( i)    # are added to the result list
  9   
 10   print result
stdout:
[65, 100]
Doba běhu: 21.3 ms
The following code uses the list comprehension do accomplish the same result.
Expand/Shrink
Zdroj: (list9-2.py)
  1   # convert the strings to integers and take only those greater than 50
  2   strings = ["12", "34", "65", "100", "1", "20", "32", "42", "5"]
  3   
  4   result = [int( string) for string in strings if int( string) > 50]
  5   print result
stdout:
[65, 100]
Doba běhu: 21.2 ms
The code below compares both approaches.
Expand/Shrink
Zdroj: (list9-3.py)
  1   # convert the strings to integers and take only those greater than 50
  2   strings = ["12", "34", "65", "100", "1", "20", "32", "42", "5"]
  3   
  4   # the good old way
  5   result = []              # we create an empty list
  6   for string in strings:
  7       i = int( string)     # we do the conversion
  8       if i > 50:               # those greater than 50...
  9           result.append( i)    # are added to the result list
 10   print result
 11   
 12   # list comprehension
 13   result = [int( string) for string in strings if int( string) > 50]
 14   print result
stdout:
[65, 100]
[65, 100]
Doba běhu: 21.3 ms
The following code-snippets show some of the common uses of the list comprehension.
Expand/Shrink
Zdroj: (list9-5.py)
  1   """conversion of integers to string"""
  2   xs = range( 10)
  3   ss = [str( x) for x in xs]
  4   print ",".join( ss)   # arguments to join must be of type string
stdout:
0,1,2,3,4,5,6,7,8,9
Doba běhu: 21.6 ms
Expand/Shrink
Zdroj: (list9-6.py)
  1   """filtering of words according to their length"""
  2   animals = ["dog", "cat", "elephant", "pig", "platypus", "hippo", "rhino"]
  3   print [animal for animal in animals if len( animal) > 3]
stdout:
['elephant', 'platypus', 'hippo', 'rhino']
Doba běhu: 20.8 ms
Expand/Shrink
Zdroj: (list9-7.py)
  1   """preparation of tuples for sorting according to last letter"""
  2   animals = ["dog", "cat", "elephant", "pig", "platypus", "hippo", "rhino"]
  3   to_sort = [(animal[-1],animal) for animal in animals]
  4   to_sort.sort()
  5   print [a[1] for a in to_sort]
stdout:
['dog', 'pig', 'hippo', 'rhino', 'platypus', 'cat', 'elephant']
Doba běhu: 21.2 ms
The sorting method used in the previous example is described in more detail in Advanced list sorting.