Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • 2
    List comprehension is the best choice. Commented Aug 30, 2009 at 3:07
  • or colors=list(c for c in colors if c != 'green') Commented Aug 30, 2009 at 9:17
  • @dugres: not quite: colors = list(...) does rebind. Alex insisted on the idea that it's better not do leave useless lists dangling in memory. Commented Aug 30, 2009 at 9:39
  • 1
    @Devin, NOT just for other references. E.g. if colors is a global doing colors= in a fuction requires an extra global colors, colors[:]= doesn't. GC of the old list does't happen instantly in all versions of Python. Etc: there's NEVER any downside in assigning to name[:], OFTEN many downsides to assigning to name (including the occasional puzzling bug where the "rarely for you" case DOES occur but you're used to the wrong way), so it's a hiding to nothing FOR the correct way, name[:]=, and AGAINST the wrong one, name=. Only one obvious way... Commented Aug 30, 2009 at 15:38
  • 3
    ...though it may not be obvious unless you're Dutch;-). Commented Aug 30, 2009 at 15:39