You can do it straight forward, like this
numlist, alist, blist = [], [], []
for item in mixlist:
if item == "a":
alist.append(item)
elif item == "b":
blist.append(item)
else:
numlist.append(item)
Or you can shorten it a little bit, like this
numlist, alist, blist = [], [], []
d = {"a": alist, "b": blist}
for item in mixlist:
d.get(item, numlist).append(item)
dict.get
method will return the value corresponding to the key. But when the key is not found, it will return the default value passed as the second parameter. So, in our case, if the key is a
or b
we will get corresponding alist
or blist
. If neither of them, then we will get numlist
and we append item
to it.
Edit: You can slightly improve the straight forward way like this
mixlist = [1, 2, 'a', 3, 'b', 'a']
numlist, alist, blist = [], [], []
nappend, aappend, bappend = numlist.append, alist.append, blist.append
for item in mixlist:
if item == "a":
aappend(item)
elif item == "b":
bappend(item)
else:
nappend(item)