Inserting values in empty list

Q. we have two lists l1 =[1,2,3,4] and l2 = [5,6,7,8] and want a output of [6,8,10,12] using given lists.

upon seeing the question, we can easily get that the output is sum between numbers at same index. i.e. l1[i]+l2[i]
Now we want show output in the form of a list, which can be achieved in two ways:

  1. we can use available space(list) and update it with the output
  2. we can create a new space(list) and store output in it

lets code for option 1:

l1 = [1,2,3,4]
l2 = [5,6,7,8]
for i in range(len(l1)):
    l1[i] = l1[i]+l2[i]
print(l1)

[6, 8, 10, 12]

Okay we are getting the output we want.
option 2 should be similar, lets try

l1 = [1,2,3,4]
l2 = [5,6,7,8]
l3=[]
for i in range(len(l1)):
    l3[i] = l1[i]+l2[i]
print(l3)
Traceback (most recent call last):
  File "9f679c9d-fb75-46a8-9dec-5303fe405255.py", line 72, in <module>
    l3[i] = l1[i]+l2[i]
IndexError: list assignment index out of range

[sub-question]
Both codes are same but why is second one giving error??
When we compare both codes, only difference is
code1 :

l1=[1,2,3,4]
l1[i] = l1[i]+l2[i]

code2:

l3 = []
l3[i] = l3[i]+l2[i]

But we are using same logic, only variables are different, so why is output different??
It is because the memory given to both the variables is different.
What does this mean??
lets see this code:

l1 = [1,2,3,4]
print(f'l1 is stored in memory at {id(l1)}')
print(f'l1[0] is stored in memory at {id(l1[0])}')
print(f'l1[1] is stored in memory at {id(l1[1])}')
print(f'l1[2] is stored in memory at {id(l1[2])}')
print(f'l1[3] is stored in memory at {id(l1[3])}')
print(f'l1[4] is stored in memory at {id(l1[4])}')
l1 is stored in memory at 139648958808624
l1[0] is stored in memory at 139648967586880
l1[1] is stored in memory at 139648967586912
l1[2] is stored in memory at 139648967586944
l1[3] is stored in memory at 139648967586976

Traceback (most recent call last):
  File "4e756777-8b9c-46e8-add1-7aa731cd9091.py", line 81, in <module>
    print(f'l1[4] is stored in memory at {id(l1[4])}')
IndexError: list index out of range

Now,

l3 = []
print(f'l3 is stored in memory at {id(l3)}')
print(f'l3[0] is stored in memory at {id(l3[0])}')
l3 is stored in memory at 140351285678640

Traceback (most recent call last):
  File "ed104bf5-dcf5-46db-a15f-769de99abd60.py", line 85, in <module>
    print(f'l3[0] is stored in memory at {id(l3[0])}')
IndexError: list index out of range

As we can see when l1 = [1,2,3,4,5] runs, it creates space for l1,l1[0],l1[1],l1[2],l1[3] and when l3 = [] runs, only space for l3 is created.

Now, lets see meaning of line l1[i] = l1[i]+l2[i],
for i =0, l1[0] = l1[0]+l2[0]
says , add values which are at location l1[0] and l2[0], update the value in l1[0] as the sum.
Now, for updating a value there should be one in first place, as there is l1[0] already created,
updating is done successfully.
same goes for i=1,2,3 and code 1 ends successfully.

But, for code 2,
for i=0, l3[0] = l1[0] + l2[0]
says , add values which are at location l1[0] and l2[0], update the value in l3[0] as the sum.
Now, for updating we need l3[0] to be existed, but as we saw only space for l3 is created not for l3[0],l3[1] and so on.
That is why the lines throws an error that there is no index 0 (i.e. index out of range) as it is not created.

Now What should we do when inserting a value to an empty list??
we use append() function as

l3.append(l1[i]+l2[i])

Hope this will help understand the condition.
If there are any doubts/corrections, feel free to comment.

@kharshavardhan31

Nice explanation, keep the pace up :relaxed:.