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

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 0.64 KB  |  hits: 10  |  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. How to remove n number of elements from a list by index in Python?
  2. g = ['1', '', '2', '', '3', '', '4', '']
  3.        
  4. g = ['1', '2', '3', '4']
  5.        
  6. >>> g = ['1', '', '2', '', '3', '', '4', '']
  7. >>> filter(None, g)
  8. ['1', '2', '3', '4']
  9.        
  10. >>> [x for x in g if x!=""]
  11. ['1', '2', '3', '4']
  12.        
  13. >>> [x for x in g if x!=""]
  14. ['1', '2', '3', '4']
  15.        
  16. >>> g = ['1', '', '2', '', '3', '', '4', '']
  17. >>> [x for x in g if x]
  18. ['1', '2', '3', '4']
  19.        
  20. new_g = [item for item in g if item != '']
  21.        
  22. >>> g = ['1', '', '2', '', '3', '', '4', '']
  23. >>> g[::2]
  24. ['1', '2', '3', '4']
  25. >>> g[1::2]
  26. ['', '', '', '']
  27. >>> del g[1::2]  #  <-- magic happens here.
  28. >>> g
  29. ['1', '2', '3', '4']