-1
def update_array(incoming: tuple, output: list = []):
  for index, element in enumerate(incoming):
    output[index] = incoming[index]
    if index % 2 == 0:
        incoming[index] = incoming[index] / 2
  return output

In the above example, line 3 is giving out of index error

output[index] = incoming[index]
IndexError: list assignment index out of range

i couldn't understand why the error occurring, trying to learn tuple and list. Thank you:-)

  • 1
    How are you calling this? If `output` is the default empty list (by the way see https://stackoverflow.com/q/1132941/3001761), then you can't assign to _any_ index in it. – jonrsharpe May 11 '21 at 16:24

1 Answers1

0

Reason for the error is that output is an empty list and occupies no space. You can append to the list instead.

output.append(incoming[index])
DeGo
  • 783
  • 6
  • 14