Try a list comprehension :
lst = [[] for _ in xrange(a)]
See below:
>>> a = 3 >>> lst = [[] for _ in xrange(a)] >>> lst [[], [], []] >>> a = 10 >>> lst = [[] for _ in xrange(a)] >>> lst [[], [], [], [], [], [], [], [], [], []] >>> # This is to prove that each of the lists in lst is unique >>> lst[0].append(1) >>> lst [[1], [], [], [], [], [], [], [], [], []] >>>
Note that this is above for Python 2.x. On Python 3.x., since xrange been removed, you will need the following:
lst = [[] for _ in range(a)]
iCodez
source share