Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

getting error as 'list' object cannot be interpreted as an integer when one list is not showing any error

I am new to Python. I separated lists into two other lists with one having starting n elements and the other list having the ending n elements.

list = [1,2,3,4,5,6,7,8]
n=3
res = list[-n:]
list_i=[]
list_j=[]
for i in range (0,n):
    list_i.append(list[i])
    print(list_i)
for j in range(0,res):
    list_j.append(res[j])
    print(list_j)

I tried to use the same for loop for the list_j but it gives TypeError: ‘list’ object cannot be interpreted as an integer. I am not clear as the type for list and res both are <class ‘list’>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In your first loop, you iterate from 0 to 3.
In the second loop, you are iterating from 0 to res. res is a list with values [6, 7, 8].

The error is saying the following: A list is not an integer. Your stop value MUST be a integer.

Hope this helps!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading