Python - Initializing Multiple Lists/Line

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!

Initialize multiple lists in python

Just use another comprehension and unpack it:

x, y, z = [[0 for i in xrange(a*b*c)] for _ in xrange(3)]

Note that [0 for i in xrange(a*b*c)] is equivalent to the simpler [0] * a*b*c.

Initialization of multiple empty lists in Python

Why the first attempt does not allocate a, b independently in memory? because they refer to same address in memory.

(a, b) = ([],)*2
print(id(a))
print(id(b))
# 4346159296
# 4346159296
(a, b) = ([]  for _ in range(2))
print(id(a))
print(id(b))
# 4341571776
# 4341914304

Initialize multiple lists in python

Just use another comprehension and unpack it:

x, y, z = [[0 for i in xrange(a*b*c)] for _ in xrange(3)]

Note that [0 for i in xrange(a*b*c)] is equivalent to the simpler [0] * a*b*c.

Python declare multiple lists

You can use a dictionary to store the values:

x=['aze','qsd','frz',...]
vars = {}
for i in x:
vars["MAX" + i] = []

Or in order for them to become real variables you can add them to globals:

x=['aze','qsd','frz',...]
for i in x:
globals()["MAX" + i] = []

You can now use:

MAXaze = [1,2,3]

Initialize complex nested lists in Python in many lines?

Inside parenthesis, including [, {, and (, you can use any formatting you want.

How to initialize a list and extend it with another list in one line?

Are not you just asking about the + list concatenation operation:

In [1]: remoteList = [2, 3, 4]

In [2]: myList = [0, 1] + remoteList

In [3]: myList
Out[3]: [0, 1, 2, 3, 4]

Works for both Python 2 and 3.


There are some slight differences between "extend" and "plus":

  • Concatenating two lists - difference between '+=' and extend()

How can I make multiple empty lists in Python?

A list comprehension is easiest here:

>>> n = 5
>>> lists = [[] for _ in range(n)]
>>> lists
[[], [], [], [], []]

Be wary not to fall into the trap that is:

>>> lists = [[]] * 5
>>> lists
[[], [], [], [], []]
>>> lists[0].append(1)
>>> lists
[[1], [1], [1], [1], [1]]

How to create multiple lists?

What you can do is use a dictionary:

>>> obj = {}
>>> for i in range(1, 21):
... obj['l'+str(i)] = []
...
>>> obj
{'l18': [], 'l19': [], 'l20': [], 'l14': [], 'l15': [], 'l16': [], 'l17': [], 'l10': [], 'l11': [], 'l12': [], 'l13': [], 'l6': [], 'l7': [], 'l4': [], 'l5': [], 'l2': [], 'l3': [], 'l1': [], 'l8': [], 'l9': []}
>>>

You can also create a list of lists using list comprehension:

>>> obj = [[] for i in range(20)]
>>> obj
[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
>>>


Related Topics



Leave a reply



Submit