I have list with different types of data (string, int, etc.). I need to create a new list with, for example, only int elements, and another list with only string elements. H
Sort the list by type, and then use groupby to group it:
groupby
>>> import itertools >>> l = ['a', 1, 2, 'b', 'e', 9.2, 'l'] >>> l.sort(key=lambda x: str(type(x))) >>> lists = [list(v) for k,v in itertools.groupby(l, lambda x: str(type(x)))] >>> lists [[9.2], [1, 2], ['a', 'b', 'e', 'l']]