Tag Archives: Python TypeError

[Solved] : Python TypeError: ‘builtin_function_or_method’ object is not iterable


In Python, When in built-in function used it must be specify with parenthesis (()) after the name of the function. If you try to run or iterate the program over a built-in method or function without parenthesis (()) the Python will throw exception as “TypeError: builtin_function_or_method is not iterable”.

Exampe TypeError: builtin_function_or_method is not iterable

Let’s consider the scenario of successful execution of a built in function in Python.

fruits = ["Papaya", "Orange", "Grapes", "Watermelon", "Apple"]
print(", ".join(fruits))

In Python, The join() is a built-in function which turns a list into a string and adds a separator between each value in a string. The output of code as below.

Output

Papaya, Orange, Grapes, Watermelon, Apple

In case while writing code, you forget the brackets (()) in built-in function then Python will throw an error. In this below scenerio will throw exception as “TypeError: builtin_function_or_method is not iterable

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}
#iterate user dictionary
for key, value in user.items:
    print("Key:", key)
    print("Value:", str(value))

Output

File "C:/Users/saurabh.gupta/Desktop/Python Example/test.py", line 7, in <module>
    for key, value in user.items:

TypeError: 'builtin_function_or_method' object is not iterable

The above example is throwing as “TypeError: ‘builtin_function_or_method‘ object is not iterable” because while using items function of dictionary programmer missed to write parenthesis (()) because for loop is iteration operation and it’s required Iterable object but items method is used without parenthesis that’s why Python is considering as object and throwing exception as “TypeError: ‘builtin_function_or_method’ object is not iterable“.

Solution

To resolve such problem related to built-in function or any function always write method with parenthesis (()).

You make correct the above program by writing items method with parenthesis as below in line no 7

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}
#iterate user dictionary
for key, value in user.items():
    print("Key:", key)
    print("Value:", str(value))

Output

Key: name
Value: Saurabh Gupta
Key: age
Value: 35
Key: city
Value: Noida

Conclusion

This type of exception “TypeError: builtin_function_or_method is not iterable” is common when user forget to use parenthesis (()) while using built-in function.

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving TypeError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved]: Python TypeError: ‘int’ object is not subscriptable


In Python some objects are subscriptable. It means that they contain, or can contain, other objects. Integers are used to store whole numbers so that are not a subscriptable object. If programmer treat an integer like a subscriptable object, an error will be raised like “TypeError : ‘int’ object is not subscriptable“.

If you noticed this is TypeError and it occurs when you try to perform operation that’s doesn’t support on object for example when you concatenate string and int then it will throw TypeError . You can more detail on [Solved] TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’.

Example : TypeError: ‘int’ object is not subscriptable

In this example, user is inserting date of birth in the format of DDMMYYYY then parse this date in Date (DD), Month(MM) and Year(YYYY).

dob = int(input("When is your date of birth? (ddmmyyyy) "))

day = dob[0:2]
month = dob[2:4]
year = dob[4:8]

print("Day:", day)
print("Month:", month)
print("Year:", year)

Output

When is your date of birth? (ddmmyyyy) 19051987
Traceback (most recent call last):

File “”, line 1, in
runfile(‘C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py’, wdir=’C:/Users/saurabh.gupta/Desktop/Python Example’)

File “C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 705, in runfile
execfile(filename, namespace)

File “C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 102, in execfile
exec(compile(f.read(), filename, ‘exec’), namespace)

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 30, in
day = dob[0:2]

TypeError: ‘int’ object is not subscriptable

In this above example, user input date of birth (19051987) and convert this date to int. Which is now whole number and not subscriptable. now in code try to parse this date of birth in form of Day (DD), Month (MM) and Year (YYYY). Because it’s whole integer number when you try to parse this integer value will through exception as “TypeError: ‘int’ object is not subscriptable

Solution

In this above program issue is because programmer is explicitly type casting the string date of birth to integer which is whole number and not subscriptable to resolve this problem programmer need to remove this explicit integer type casting as below.

dob = input("When is your date of birth? (ddmmyyyy) ")

day = dob[0:2]
month = dob[2:4]
year = dob[4:8]

print("Day:", day)
print("Month:", month)
print("Year:", year)

Output

When is your date of birth? (ddmmyyyy) 19051987
Day: 19
Month: 05
Year: 1987

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving KeyError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved] TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’


In Python, when we perform any operation on variables of different datatypes, the data of one variable will be converted to a higher datatype among the two variables and the operation is completed. This conversion is done by interpreter automatically and it is known as implicit type conversion. But Python does not support implicit type conversion of two different data types, and it will throw an error.

Example:

num1=20
num2="30"
result=num1+num2
print(result)

Output:

Traceback (most recent call last):
  File “D:SaurabhPythonsrctest.py”, line 3, in <module>
    result=num1+num2
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

Solution

If we must avoid this, then we have to explicitly convert the datatype of one variable into the required data type to complete the operation. This is known as explicit type conversion.

Example:

num1=20
num2="30"
result=num1+int(num2)
print(result) 

Output:

50

Note:

Programming languages define their own rules for implicit and explicit conversions. These rules will change from language to language.

Similarly, one has to be careful in explicit conversions as well. For example,

  • Converting a floating-point value to integer would result in loss of decimal point values.
  • A larger data type if converted to smaller data type will result in loss of data as the number will be truncated.

Python: Type Conversion (Implicit & Explicit)


In Python, when we perform any operation on variables of different datatypes, the data of one variable will be converted to a higher datatype among the two variables and the operation is completed. This conversion is done by interpreter automatically and it is known as implicit type conversion. But Python does not support implicit type conversion of two different data types, and it will throw an error.

Example:

num1=20
num2="30"
result=num1+num2
print(result)

Output:

Traceback (most recent call last):
  File “D:\Saurabh\Python\src\test.py”, line 3, in <module>
    result=num1+num2
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

Solution

If we must avoid this, then we have to explicitly convert the datatype of one variable into the required data type to complete the operation. This is known as explicit type conversion.

Example:

num1=20
num2="30"
result=num1+int(num2)
print(result) 

Output:

50

Note:

Programming languages define their own rules for implicit and explicit conversions. These rules will change from language to language.

Similarly, one has to be careful in explicit conversions as well. For example,

  • Converting a floating-point value to integer would result in loss of decimal point values.
  • A larger data type if converted to smaller data type will result in loss of data as the number will be truncated.