Mar-30-2020, 03:13 PM
Hi,
I am getting an TypeError, and not sure why? My goal is to write on the second and third position of a list . I can do it using a for loop but I would like to avoid the for loop by indexing a range of elements directly on the list. How can I do it?
>>>list=[1,3,4,30,10,20]
>>>for i in range(1,3,1):
list[i] = 0
>>>list
[1, 0, 0, 30, 10, 20]
>>>list[1:3] = 1
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: can only assign an iterable
I am getting an TypeError, and not sure why? My goal is to write on the second and third position of a list . I can do it using a for loop but I would like to avoid the for loop by indexing a range of elements directly on the list. How can I do it?
>>>list=[1,3,4,30,10,20]
>>>for i in range(1,3,1):
list[i] = 0
>>>list
[1, 0, 0, 30, 10, 20]
>>>list[1:3] = 1
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: can only assign an iterable