What is the fastest way to split a list into multiple sublists based on conditions? Each condition represents a separate sublist.
One way to split a listOfObjects
into sublists (three sublists for demonstration, but more are possible):
listOfObjects = [.......]
l1, l2, l3 = [], [], []
for l in listOfObjects:
if l.someAttribute == "l1":
l1.append(l)
elif l.someAttribute == "l2":
l2.append(l)
else:
l3.append(l)
This way does not seem pythonic at all and also takes quite some time. Are there faster approaches, e.g. using map
?
There is a similar question, but with only one condition, i.e., two result lists: How to split a list based on a condition?