Python – Initializing Multiple Lists/Line

alist, blist, clist, dlist, elist = ([] for i in range(5))

The downside of above approach is, you need to count the number of names on the left of = and have exactly the same number of empty lists (e.g. via the range call, or more explicitly) on the right hand
side.

The main thing is, don’t use something like

alist, blist, clist, dlist, elist = [[]] * 5

nor

alist = blist = clist = dlist = elist = []

which would make all names refer to the same empty list!

Leave a Comment