1

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:

  1. why ptyhon returns blank list when accessing like l[3:1]?
  2. why it gives error when using l[3:1] = 4 but works fine using l[3:1]='a'?
  3. Why it is giving error instead of adding new value at 5th index?
Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
Vishal Patel
  • 1,715
  • 16
  • 26
  • Regarding point 3, you are trying to access memory not yet assigned to the list, hence the error. One way to get around that is `l.append(5)`. – Henry Sep 29 '16 at 10:37
  • @Padraic Cunningham: This is not duplicate of this question. there is now answer of my 2 and 3rd question. so Please remove duplicate mark. – Vishal Patel Sep 29 '16 at 11:05

4 Answers4

2
  1. why python returns blank list when accessing like l[3:1]?

list[i:j] means you want sliced list with value from 'i'th index to upto 'j'th index (not including jth index). For example:

>>> my_list = [1, 2, 3, 4, 5]
>>> my_list[2:3]
[3]

In your case i > j, hence list is empty.

  1. why it gives error when using l[3:1] = 4 but works fine using l[3:1]='a'?

l[3:1] is the sliced list. You can a assigned it only with list. since, python str are considered as the list of chars. That is why you are able to assigned it.

  1. Why it is giving error instead of adding new value at 5th index?

Even though the length of your list is 5 but since the index of the list starts with 0, you can access element of list only till len(list) - 1. In case you want to add item at the end of list, you need to do .append() on the list. Below is the example to show this:

>>> my_list = [1, 2, 7, 9, 8, 4, 5]
>>> len(my_list)
7       # <-- Length as 7
>>> my_list[6]  
5       # <-- value of index at length(list) - 1, which is the last value of list
>>> my_list[7]   # <-- index as length of list, resulted in 'IndexError'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> my_list.append(9)   # <-- Adds an item at the of list
>>> len(my_list)    # length incremented by 1
8
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
1

as l[3:1] is a Sequence hence you can assign only a Sequence to it thus, the reason l[3:1] works because 'a' itself is a sequence but 4 is a normal integer.

armak
  • 560
  • 5
  • 13
1
  1. Why does python return a blank list when accessing like l[3:1]?

Slicing works as follows:

l[start:end] # start to end-1
l[start:]    # start to rest of list
l[:end]      # beginning of list to end-1
l[:]         # copy complete list

As you are passing l[3:1] i.e start from 3rd index and end at 1-1 i.e 0th index, that means start index > end index, so empty list will be displayed. Either you can pass l[1:3] or any negative indexes (ex: l[-3:-1]).

  1. Why does it throw an error when using l[3:1] = 4 but works fine using l[3:1]='a'?

In slicing retrieval, you will get a list (sequence)

In same way, in case of slicing assignment, you can pass sequence. Here string is sequence of characters and so working for string and not for integer

In case of integer, you can try:

l[3:1] = [4]
  1. Why does it throw an error instead of adding new value at 5th index?

list contain only 4 elements. l = [1,2,3,4]

Here, you can not access or assign more than index 3 (index starts from 0). In your scenario, you can append data.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
0

First question - You are trying to slice the list from two different ways.

Given l = [1, 2, 3, 4]

l[3:] slices the list from element with index 3 and onwards, leaving you with [4]

l[:1] slices the list from element with index 1 and backwards, leaving you with [1].

Now if you slice it both ways, you will have an empty list, there's nothing in that range.

Second question - this one is kind of odd. In theory string behaves like a list with each character being an item in that list, so that's why you can only enter 'a' and 4 being an integer throws an error. I don't exactly know why, but [3:1] inserts the new list (characters of the string) after 3rd element. If you leave out the [:1], it will replace the list from 3rd element instead, leaving you with [1, 2, 3, 'a']

Third question - You can only access indexes of a list that already exist. 5th index doesn't exist for you yet. You have to create it. That's either by append, insert etc. Then you can modify it with l[5]

iScrE4m
  • 882
  • 1
  • 12
  • 31