0

I wrote a code to do some kind of algorithm. For this I need a array of array's like: d = [[1,4,5], [0,3,6,1], [7,3]]. I made an randomizer to get random data to work with. For this randomizer i need to start with an empty list like: d = [[][][]] This works well, but when i change the number of rows in my algorithm i have to change this by hand. Is there a way to change the number automatically? I tried:

d = []*10

and

d = []
for i in range(1, n+1):
    d[i] = []

but both do not work.

  • 2
    Do `d = [[] for _ in range(n)]`. Your second example will also work if you do `d.append([])` instead of `d[i] = []`. The first example will *almost* do what you want if you do `d = [[]] * 10`, but you'll actually end up with 10 references to one list and have some very mystifying bugs as a result. – Samwise Apr 20 '22 at 16:54
  • 1
    note, these are *lists* not arrays – juanpa.arrivillaga Apr 20 '22 at 16:54
  • Thanks! It works – Maarten Kant Apr 20 '22 at 16:55

0 Answers0