Search code examples
pythonlisttypeerror

I am trying to pop a list but it is returning a Type Error?


I am trying to create a list of words with 5 characters. I am using this approach

word = pd.read_csv('words.csv').values.tolist()
for x in word:
    if len(x) != 5:
        word.pop(x)word = pd.read_csv('words.csv').values.tolist()
for x in word:
    if len(x) != 5:
        word.pop(x)

But this set of codes returns this particular error

word.pop(x)
TypeError: 'list' object cannot be interpreted as an integer

Can someone help me understand the Type Error and Troubleshoot it


Solution

  • pop is a function to remove the ith element from a list. I guess from your code that x is not an integer. You can check this with type(x). I think you want the remove function, which

    Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.

    source: https://docs.python.org/3/tutorial/datastructures.html