What's the best way, both aesthetically and from a performance perspective, to split a list of items into multiple lists based on a conditional? The equivalent of:
good = [x for x in mylist if x in goodvals]
bad = [x for x in mylist if x not in goodvals]
is there a more elegant way to do this?
Update: here's the actual use case, to better explain what I'm trying to do:
# files looks like: [ ('file1.jpg', 33L, '.jpg'), ('file2.avi', 999L, '.avi'), ... ]
IMAGE_TYPES = ('.jpg','.jpeg','.gif','.bmp','.png')
images = [f for f in files if f[2].lower() in IMAGE_TYPES]
anims = [f for f in files if f[2].lower() not in IMAGE_TYPES]
str.split(), to split the list into an ordered collection of consecutive sub-lists. E.g.split([1,2,3,4,5,3,6], 3) -> ([1,2],[4,5],[6]), as opposed to dividing a list's elements by category. - StewIMAGE_TYPES = set('.jpg','.jpeg','.gif','.bmp','.png'). n(1) instead of n(o/2), with practically no difference in readability. - ChaimG