Apr-27-2022, 01:17 AM
(This post was last modified: Apr-27-2022, 01:23 AM by Pedroski55.)
I don't know anything about dict.setdefault() or where it might be useful.
You could achieve the same result like this:
You could achieve the same result like this:
mylist = ["a", "b", "c", "a", "c"] # make a dictionary of empty lists mydict = {l:[] for l in mylist} # populate the lists for v, k in enumerate(mylist): mydict[k].append(v)How to put these 2 steps in a one-liner, I don't know!