Home List in for loop and TypeError: 'range' object does not support item assignment
Reply: 0

List in for loop and TypeError: 'range' object does not support item assignment

JakubM
1#
JakubM Published in 2018-01-11 12:58:38Z

I want to iterate list in a for loop in a specific range.

When I assign a[i]= 0, I receive

TypeError: 'range' object does not support item assignment"

so I replaced i with int(i) and it works.

But how can I avoid using int(i) in the for loop?

Here is my code:

a = [0, 0, 0, 0, 0, 0, 0]
print(a)
a[0] = 1  # this is fine
print(a)

for i in range(2, 7):        
    a[i] = 0       # ERROR
    a[int(i)] = 0  # OK
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299838 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO