Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 11th, 2012  |  syntax: None  |  size: 0.42 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. proper use of list comprehensions - python
  2. >>> a = [1, 2, 3, 4, 5]
  3. >>> [i for i in a if i > 2]
  4. [3, 4, 5]
  5.        
  6. >>> a = [1, 2, 3, 4, 5]
  7. >>> b = []
  8. >>> [b.append(i) for i in a]
  9. [None, None, None, None, None]
  10. >>> print b
  11. [1, 2, 3, 4, 5]
  12.        
  13. for i in a:
  14.     b.append(i)
  15.        
  16. b = [i for i in a]
  17.        
  18. for i in a:
  19.     b.append(i)
  20.        
  21. b += (i for i in a)
  22.        
  23. b += a
  24.        
  25. b += map(somefunc, a)
  26.        
  27. b = []
  28. a = [1, 2, 3, 4, 5]
  29. b.extend (a)