Last modified: Dec 09, 2025 By Alexander Williams

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

Python errors can be confusing for beginners. The AttributeError is common. It happens when you try to use a method on the wrong data type. This article explains the 'list' object has no attribute 'items' error. We will show you why it occurs and how to fix it.

Understanding the Error Message

Let's break down the error message. AttributeError means you called an attribute or method that does not exist for that object. The part 'list' object tells you the type of your variable. The part has no attribute 'items' is the key. It says you tried to call .items() on a list. The .items() method belongs to dictionaries, not lists.

Why Does This Error Happen?

This error is a data type confusion. You have a list but are treating it like a dictionary. Lists are ordered collections accessed by index. Dictionaries are key-value pairs. The .items() method returns dictionary keys and values. Lists do not have this method. Trying to use it causes the AttributeError.

Common Cause: Expecting a Dictionary

Often, code expects data from an API or function to be a dict. Sometimes the returned data is a list instead. Your code then fails when calling .items(). Another common cause is variable reassignment. A variable meant to be a dict might become a list later in the code.

How to Diagnose the Problem

First, identify the variable causing the error. Check the full traceback from Python. It shows the line number. Use the type() function to check the variable's type. Also, use print() to see its contents. This confirms if it's a list or a dictionary.


# Example code that causes the error
my_data = [1, 2, 3]  # This is a list
print(type(my_data)) # Check the type
my_data.items()      # This line will cause the error

# Output from running the code

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

Solutions to Fix the Error

You must ensure you are working with the correct data type. The solution depends on your goal. Do you need a list or a dictionary? The fix changes based on your answer.

Solution 1: Convert a List of Tuples to a Dictionary

Sometimes your list contains key-value pairs as tuples. You can convert it to a dict. Then you can safely use .items(). Use the dict() constructor for this conversion.


# A list of tuples (like key-value pairs)
list_of_tuples = [('a', 1), ('b', 2), ('c', 3)]
print("Original list:", list_of_tuples)
print("Type:", type(list_of_tuples))

# Convert list to dictionary
my_dict = dict(list_of_tuples)
print("\nConverted dict:", my_dict)
print("Type:", type(my_dict))

# Now .items() will work
for key, value in my_dict.items():
    print(f"Key: {key}, Value: {value}")

Original list: [('a', 1), ('b', 2), ('c', 3)]
Type: 

Converted dict: {'a': 1, 'b': 2, 'c': 3}
Type: 
Key: a, Value: 1
Key: b, Value: 2
Key: c, Value: 3

Solution 2: Iterate Over the List Correctly

If you need to loop through a list, do not use .items(). Use a for loop directly on the list. Or use enumerate() if you need an index. This is the correct way to handle list data.


my_list = ['apple', 'banana', 'cherry']

# Correct way to iterate a list
for item in my_list:
    print(item)

# If you need an index, use enumerate
for index, item in enumerate(my_list):
    print(f"Index {index}: {item}")

apple
banana
cherry
Index 0: apple
Index 1: banana
Index 2: cherry

Solution 3: Check Data Source and Type

If data comes from an external source, verify its type. Use conditional checks like isinstance(). Handle both list and dictionary cases. This makes your code more robust and prevents errors.


data = get_data_from_api()  # This function might return a list or dict

# Check type before using .items()
if isinstance(data, dict):
    for key, value in data.items():
        process_key_value(key, value)
elif isinstance(data, list):
    for item in data:
        process_list_item(item)
else:
    print(f"Unexpected data type: {type(data)}")

Related AttributeError Issues

Mixing up data types causes many AttributeErrors. For example, trying to use .append() on a string causes a Fix Python AttributeError: 'str' object has no attribute 'append' error. Similarly, calling .lower() on an integer leads to a Fix AttributeError: 'int' object has no attribute 'lower' issue. Another common mix-up is expecting list methods on strings, like the Fix AttributeError: 'list' object has no attribute 'split' error.

Best Practices to Avoid This Error

Follow these tips to prevent data type errors. First, use meaningful variable names. Names like user_dict or item_list clarify the type. Second, add type hints in your function definitions. This documents what type of data is expected. Third, write unit tests. Tests can catch when a function returns the wrong type.

Conclusion

The AttributeError 'list' object has no attribute 'items' is a clear sign of data type confusion. You tried to use a dictionary method on a list. The fix is to ensure you are using the correct data structure. Convert your list to a dict if needed. Or iterate over the list correctly. Always check the type of your variables with type() or isinstance(). Understanding Python's data types is key to solving this and similar errors.