Last modified: Dec 09, 2025 By Alexander Williams

Fix Python AttributeError: 'int' object has no attribute 'items'

Python errors can stop your code. The AttributeError is common. It happens when you call a method on the wrong object type.

This error means you tried to use .items() on an integer. The .items() method only works for dictionaries.

This guide explains the error. You will learn why it happens. We will show you how to fix it step by step.

Understanding the AttributeError

An AttributeError signals a problem. You are trying to access an attribute. Or you are calling a method.

The object does not support that operation. In this case, an integer was used. The .items() method was called on it.

Integers are simple numbers. They do not have key-value pairs. Dictionaries store data in pairs. Only dictionaries have the .items() method.

This mistake often comes from variable confusion. Your variable might not be what you think. It may be an integer instead of a dict.

Common Causes of the Error

Let's look at why this happens. The root cause is usually simple. You expect a dictionary but have an integer.

Cause 1: Variable Reassignment

You might change a variable's value. Later code expects the old type. This causes the error.


# Example of variable reassignment
my_data = {"name": "Alice", "age": 30}
print(my_data.items())  # This works

# Later, we reassign my_data to an integer
my_data = 42
print(my_data.items())  # This causes AttributeError

Traceback (most recent call last):
  File "script.py", line 7, in 
    print(my_data.items())
AttributeError: 'int' object has no attribute 'items'

Cause 2: Function Returning Unexpected Type

A function may return different types. Your code might assume it's always a dict. Sometimes it returns an integer.


def get_value(key):
    data = {"x": 10, "y": 20}
    # If key not found, return 0 (an integer)
    return data.get(key, 0)

result = get_value("z")  # Returns 0, an integer
print(result.items())    # AttributeError!

Cause 3: Incorrect Data Parsing

You might parse JSON or user input. The parsing might fail. You could end up with a number instead of a dict.

Always check the type of your data. Do not assume it is a dictionary.

How to Fix the Error

Fixing this error requires careful checking. You must ensure you are using a dictionary. Below are proven solutions.

Solution 1: Check Variable Type Before Calling .items()

Use the type() function or isinstance(). This confirms your variable is a dict.


my_var = 100  # This is an integer

if isinstance(my_var, dict):
    for key, value in my_var.items():
        print(key, value)
else:
    print(f"Expected a dictionary, but got {type(my_var).__name__}")

Expected a dictionary, but got int

This prevents the error. Your code handles the wrong type gracefully.

Solution 2: Debug and Print Variable Values

Print the variable and its type. This shows you what you are really working with.


config = load_configuration()  # Unknown function
print(f"Value: {config}")
print(f"Type: {type(config)}")

# Only proceed if it's a dictionary
if isinstance(config, dict):
    process_items(config.items())

Debugging reveals the true data type. It is a simple but powerful technique.

Solution 3: Ensure Consistent Variable Types

Review your code logic. Make sure a variable is always a dictionary. Avoid reassigning it to an integer.

Use separate variables for different data types. This keeps your code clear.


# Good practice: Use separate variables
user_dict = {"id": 101, "role": "admin"}
user_id = 101  # Separate variable for the ID integer

# Now you can safely use .items() on user_dict
for key, val in user_dict.items():
    print(f"{key}: {val}")

Related AttributeError Issues

This error is part of a family. Confusing data types causes many AttributeErrors.

For example, you might see Fix Python AttributeError: 'list' object has no attribute 'lower'. This happens when you treat a list like a string.

Another common one is Fix Python AttributeError: 'dict' object has no attribute 'split'. You try to split a dictionary as if it were a string.

Also, Fix Python AttributeError 'str' Object Has No Attribute 'keys' occurs. You call .keys() on a string instead of a dict.

The solution is always the same. Know your data types. Use the correct methods for each type.

Best Practices to Avoid AttributeError

Follow these tips. They will help you write robust Python code.

Use Type Hints. They make your code's intent clear. They help tools catch type mismatches early.


from typing import Dict, Any

def process_data(data: Dict[str, Any]) -> None:
    # The type hint tells us 'data' should be a dict
    for k, v in data.items():
        print(k, v)

Validate External Data. Data from files, APIs, or users can be wrong. Check its type before using it.

Write Unit Tests. Tests can catch type errors. They ensure your functions handle all input cases.

Conclusion

The AttributeError 'int' object has no attribute 'items' is straightforward. You called a dictionary method on an integer.

To fix it, identify why your variable is an integer. Use type checking and debugging. Ensure your variables hold the expected data types.

Understanding Python's data types is key. This prevents many common errors. Always know what type of object you are working with.

Keep your code clean and logical. Use the solutions in this guide. Your programs will be more reliable and easier to debug.