-1
n = int(len(network))
num=[]

for i in range(n):
    for j in range(n):

        num[i].append(int (between_lists(network[i],network[j])))
return num

this error message I get

 num[i].append(int (between_lists(network[i],network[j])))
IndexError: list index out of range
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
wise john
  • 5
  • 3
  • Hint: where the code says `num[i].append`, notice how it does **not** say `num[i][j] =`? Do you understand why it would be wrong to try to add elements to `num[i]` that way? Now, think carefully about the `num[i]` part. Doesn't the same problem apply there? After all, what is `num` before the loop? – Karl Knechtel Oct 15 '22 at 02:32

1 Answers1

0

Any of your three indexing operations could be the cause, but num is definitely an empty list, so you can't index it at all. Did you perhaps mean to append to num itself, not to a sublist within num (which has no inner lists at all)?

num.append(int (between_lists(network[i],network[j])))
 # ^ removed [i]  

Update: Based on your comments, you want a new sublist for each run of the outer loop, so what you really wanted is:

for i in range(n):
    num.append([])  # Put in new sublist
    for j in range(n):
        num[i].append(int(between_lists(network[i],network[j])))
return num
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • it works, but I am try to get results like this [[3, 0, 1, 1, 1, 0, 2, 1, 2, 3], [0, 5, 3, 2, 2, 1, 1, 1, 3, 0], [1, 3, 5, 3, 2, 1, 1, 1, 2, 1], [1, 2, 3, 4, 1, 1, 2, 1, 1, 1], [1, 2, 2, 1, 4, 0, 2, 2, 2, 1], [0, 1, 1, 1, 0, 1, 0, 0, 0, 0], [2, 1, 1, 2, 2, 0, 4, 3, 2, 2], [1, 1, 1, 1, 2, 0, 3, 3, 1, 1], [2, 3, 2, 1, 2, 0, 2, 1, 5, 2], [3, 0, 1, 1, 1, 0, 2, 1, 2, 4]] – wise john Oct 15 '22 at 01:54
  • but this is what I get [5, 3, 2, 2, 1, 1, 1, 3, 0, 3, 5, 3, 2, 1, 1, 1, 2, 1, 2, 3, 4, 1, 1, 2, 1, 1, 1, 2, 2, 1, 4, 0, 2, 2, 2, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 2, 2, 0, 4, 3, 2, 2, 1, 1, 1, 2, 0, 3, 3, 1, 1, 3, 2, 1, 2, 0, 2, 1, 5, 2, 0, 1, 1, 1, 0, 2, 1, 2, 4] – wise john Oct 15 '22 at 01:56