53

The playSound function is taking a list of integers, and is going to play a sound for every different number. So if one of the numbers in the list is 1, 1 has a designated sound that it will play.

def userNum(iterations):
  myList = []
  for i in range(iterations):
    a = int(input("Enter a number for sound: "))
    myList.append(a)
    return myList
  print(myList)

def playSound(myList):
  for i in range(myList):
    if i == 1:
      winsound.PlaySound("SystemExit", winsound.SND_ALIAS)

I am getting this error:

TypeError: 'list' object cannot be interpreted as an integer

I have tried a few ways to convert the list to integers. I am not too sure what I need to change. I am sure that there is a more efficient way of doing this. Any help would be very greatly appreciated.

1
  • 3
    range(myList) is basically an integer. So for eg. if myList contains 7 items then that would mean {i in 7}, which does not make sense over here. Rather simply traverse myList using {for i in mylist}. Commented Apr 13, 2020 at 14:41

15 Answers 15

62

Error messages usually mean precisely what they say. So they must be read very carefully. When you do that, you'll see that this one is not actually complaining, as you seem to have assumed, about what sort of object your list contains, but rather about what sort of object it is. It's not saying it wants your list to contain integers (plural)—instead, it seems to want your list to be an integer (singular) rather than a list of anything. And since you can't convert a list into a single integer (at least, not in a way that is meaningful in this context) you shouldn't be trying.

So the question is: why does the interpreter seem to want to interpret your list as an integer? The answer is that you are passing your list as the input argument to range, which expects an integer. Don't do that. Say for i in myList instead.

Sign up to request clarification or add additional context in comments.

4 Comments

or for i in range(len(myList)) if looking to iterate over indices rather than elements themselves.
or for index, item in enumerate(myList) to get you both, IMO more readably
The same error appears if one uses: for index, (item1, item2) in enumerate(list1, list2). To correct this, make use of zip(): for index, (item1, item2) in enumerate(zip(list1, list2))
@JavierTG That’s right. In this case enumerate() wants to interpret the second argument you give it as “integer index label of first item” so again, a list won’t work in that position.
24

For me i was getting this error because i needed to put the arrays in paratheses. The error is a bit tricky in this case...

ie. concatenate((a, b)) is right

not concatenate(a, b)

hope that helps.

1 Comment

Yes! np.concatenate requires double parentheses for the arrays: numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")
12

The error is from this:

def playSound(myList):
  for i in range(myList): # <= myList is a list, not an integer

You cannot pass a list to range which expects an integer. Most likely, you meant to do:

 def playSound(myList):
  for list_item in myList:

OR

 def playSound(myList):
  for i in range(len(myList)):

OR

 def playSound(myList):
  for i, list_item in enumerate(myList):

Comments

9

range is expecting an integer argument, from which it will build a range of integers:

>>> range(10)
range(0, 10)
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

Moreover, giving it a list will raise a TypeError because range will not know how to handle it:

>>> range([1, 2, 3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object cannot be interpreted as an integer
>>>

If you want to access the items in myList, loop over the list directly:

for i in myList:
    ...

Demo:

>>> myList = [1, 2, 3]
>>> for i in myList:
...     print(i)
...
1
2
3
>>>

Comments

6

remove the range.

for i in myList

range takes in an integer. you want for each element in the list.

Comments

5

You should do this instead:

for i in myList:
    # etc.

That is, remove the range() part. The range() function is used to generate a sequence of numbers, and it receives as parameters the limits to generate the range, it won't work to pass a list as parameter. For iterating over the list, just write the loop as shown above.

Comments

5

since it's a list it cannot be taken directly into range function as the singular integer value of the list is missing.

use this

for i in range(len(myList)):

with this, we get the singular integer value which can be used easily

Comments

2

In playSound(), instead of

for i in range(myList):

try

for i in myList:

This will iterate over the contents of myList, which I believe is what you want. range(myList) doesn't make any sense.

Comments

2
def userNum(iterations):
    myList = []
    for i in range(iterations):
        a = int(input("Enter a number for sound: "))
        myList.append(a)
    print(myList) # print before return
    return myList # return outside of loop

def playSound(myList):
    for i in range(len(myList)): # range takes int not list
        if i == 1:
            winsound.PlaySound("SystemExit", winsound.SND_ALIAS)

Comments

1
n=input().split()
ar=[]
for i in n:
    if i not in ar:
        ar.append(i)
print(*ar)

We usually pass string as a integer... for that we have to type "n"

Comments

0

when we facing like this error:
TypeError: st object cannot be interpreted as an st

it usually because we use X instead len(X) in for loops

#error 
for i in range(df.index):
    pass

#currect
for i in range( len(df.index) ):
    pass

Comments

0

list cannot be interpreted as an integer when using range

for i in range(list): -> will not work

for i in list: -> will work

Comments

0

I got this error while using a code of my coworker where he loaded some data from a configuration file. I didn't notice it, but the "step" parameter was the issue, which was a list instead of an integer.

Problem was:

for i in range(0, len(my_list), step = my_other_list)

instead of:

for i in range(0, len(my_list), step = integer_nb)

Comments

0

TypeError: 'list' object cannot be interpreted as an integer

The error is accurate,not only check in the parameters or attributes, also check in the while or for loops present. Because we sometimes accidently code the list in the range instead of len of list.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

This was the top answer when I was searching for the same error message. Mine was related to trying to pop or del a list item, like:

del my_list[item]

or

my_list.pop(item)

and ultimately I realized here that item is an item and not an index. If you want to use it this way, like you would to delete an item from a dictionary, you need to get the item index:

del my_list[my_list.index(item)]

or

pop my_list[my_list.index(item)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.