One way (which does not require to define possible classes in chain of if-s) is to use defaultdict and class name:
>>> import collections >>> lst = ['abc', 1, 2, 3, 4.3, [42]] >>> types = collections.defaultdict(list) >>> for item in lst: ... types[type(item).__name__].append(item) ... >>> types defaultdict(list, {'str': ['abc'], 'int': [1, 2, 3], 'float': [4.3], 'list': [[42]]}) >>> for k, v in types.items(): ... print(f'{k} --> {v}') ... str --> ['abc'] int --> [1, 2, 3] float --> [4.3] list --> [[42]] >>> types['int'] [1, 2, 3]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.