I have a dictionary, the values ​​of which are represented by a list of strings containing integers: {35: ["85", "95"]} .

Using this code, I wanted to replace all string values ​​with similar integers:

  TempList = [] for key in graph: for value in graph[key]: if isinstance(value, str): TempList.extend(int(value)) graph.setdefault(key, TempList) 

But TypeError: 'int' object is not iterable gets out TypeError: 'int' object is not iterable I don’t understand why this error gets out and how to fix it.

Thanks in advance for your help.

    1 answer 1

    TempList.extend() expects an iterable object (that is, an object whose methods are implemented: __iter__() or __getitem__() ) as an argument

     In [85]: TempList.extend? Docstring: L.extend(iterable) -> None -- extend list by appending elements from the iterable Type: builtin_function_or_method 

    try using: TempList.append(...)

    • Thank you, this cleared up the error. But the values ​​are still string. That is, TempList is not set for key. Given that in the TempList integer values. I do not understand why - Dan
    • Changed graph.setdefault (key, TempList) to graph [key] = TempList and earned. - Dan