i have added one list like below:
>>> l = [1,2,3,4]
>>> len(l)
4
so when i accessed
l[3:1]
python has return blank list like []
l[3:1]=4
it returns error like
>>> l[3:1] = 4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only assign an iterable
but when i used l[3:1] = 'a'
. it successfully run and gives new list as below:
>>> l[3:1] = 'a'
>>> l
[1, 2, 3, 'a', 4]
>>>
now i have length of 5. now i want to add new element at 5th index so i write
>>> l[5] = 5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>>
my questions are follow:
- why ptyhon returns blank list when accessing like l[3:1]?
- why it gives error when using
l[3:1] = 4
but works fine usingl[3:1]='a'
? - Why it is giving error instead of adding new value at 5th index?