Attributeerror: list object has no attribute items [SOLVED]

Hey, are you stuck and don’t know how to fix the “attributeerror: list object has no attribute items” error message?

Worry no more, because in this article we are going to explore numerous solutions that will definitely help you resolve the error.

Apart from the solutions, you’ll also learn what this error is all about and why it occurs in your code.

So, keep on reading until you finally get rid of the attributeerror: ‘list’ object has no attribute ‘items’ error message and continue working with your code.

What is “attributeerror: list object has no attribute items” error message?

The error message “attributeerror: list object has no attribute items” is a common error in Python programming.

That is raised when you are trying to access the items method of a list object, which is not a valid attribute of a list.

It is because the “items” method is specific to dictionary objects and is not available for list objects.

What are the root causes of this error?

  • The most common cause of this error is a programming mistake, where a programmer assumes that the object is a dictionary instead of a list and tries to call the items() method on it.
  • Another possible cause is a typo or a syntax error in the code, where the method name is misspelled or used incorrectly.

How to fix “attributeerror: list object has no attribute items”

The following are effective solutions you can use to troubleshoot the list’ object has no attribute ‘items’ error message:

1. Convert the list to a dictionary and use the “.items()” method

For example:

sample_list = [1 ,2 ,3 , 4, 5]

my_dict = dict.fromkeys(sample_list)

print(my_dict.items())

In this example code, we convert the list to a dictionary using the “dict.fromkeys()” method. Then, we use the “.items()” method to access the items of the dictionary.

Since the “items” method is specific to dictionary objects, we can use it after converting the list to a dictionary.

Output:

dict_items([(1, None), (2, None), (3, None), (4, None), (5, None)])

Another example:

my_list = [('itsourcecode', 1), ('sourcecode', 2), ('sourcecodehero', 3)]
my_dict = dict(my_list)
print(my_dict.items())

Output:

dict_items([('itsourcecode', 1), ('sourcecode', 2), ('sourcecodehero', 3)])

2. Use a for loop to iterate over the list

For example:

sample_list = [1, 2, 3, 4, 5]

for item in sample_list:
    print(item)

In this example, we use a for loop to iterate over the list and print each item. Since the “items” method is not available for list objects, we can’t use it directly.

Instead, we can access the items of a list using a loop.

Output:

1
2
3
4
5

3. Use list comprehension

For example:

sample_list = [1, 2, 3, 4, 5]

sample_list_items = [(index, item) for index, item in enumerate(sample_list)]

print(sample_list_items)

In this example code, we use list comprehension to create a list of tuples containing the index and item of each element in the list.

This is another alternative method to access the items of a list without using the “items” method.

Output:

[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]

4. Use enumerate() functon

Use the enumerate() function to get the index and item of each element in the list.

sample_list = [1, 2, 3, 4, 5]

for index, item in enumerate(sample_list):
    print(index, item)

In this example, we use the “enumerate()” function to get the index and item of each element in the list.

This is an alternative method to iterate over the list and access its items without using the “items” method.

Output:

0 1
1 2
2 3
3 4
4 5

Another example:

my_list = ['itsourcecode', 'sourcecode', 'sourcecodehero']
for index, item in enumerate(my_list):
    print(f"{index}: {item}")

Output:

0: itsourcecode
1: sourcecode
2: sourcecodehero

5. Create a custom function to access the items of a list

def get_list_items(sample_list):
    items = []
    for item in sample_list:
        items.append((sample_list.index(item), item))
    return items

sample_list = [1, 2, 3, 4, 5]

print(get_list_items(sample_list))

In this example code, we create a custom function called “get_list_items” that takes a list as input and returns a list of tuples containing the index and item of each element in the list.

Output:

[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]

Related Articles for Python Errors

Conclusion

By executing the different solutions that this article has given, you can easily fix the “attributeerror: list object has no attribute items ” error message when working in Python.

We are hoping that this article provides you with sufficient solutions; if yes, we would love to hear some thoughts from you.

Thank you very much for reading to the end of this article. Just in case you have more questions or inquiries, feel free to comment, and you can also visit our website for additional information.

Built-in type AttributeError patterns

AttributeErrors on built-in types (dict, list, str, int) almost always indicate a variable overwritten with the wrong type, a version-removed method, or attribute-vs-method confusion.

Common triggers

  • Variable is the wrong type. my_list.keys() fails because my_list is a list, not a dict. Print type first.
  • Method removed in Python 3. dict.has_key(), list.sort(cmp=...), and others were dropped.
  • String method returns str, not list. "hello".split() returns a list. Chaining as if str fails.
  • Int has no length. len(5) raises TypeError, but (5).len() raises AttributeError.

Diagnostic pattern

# BAD — assumed dict but variable is list
data = [{"name": "Alice"}, {"name": "Bob"}]
for key in data.keys():  # AttributeError: 'list' object has no attribute 'keys'
    print(key)

# GOOD — iterate list correctly
for item in data:
    print(item["name"])

# BAD — dict.has_key removed in Python 3
if my_dict.has_key("name"):  # AttributeError
    ...

# GOOD — use in operator
if "name" in my_dict:
    ...

Best practices

  • Print type(x) when debugging. Confirms what Python actually has.
  • Use isinstance() checks. Guard code paths by type.
  • Use type hints. mypy catches most type mismatches statically.
  • Prefer explicit conversion. list(iterable), dict(pairs), str(value).

Frequently Asked Questions

What is Python AttributeError and what causes it?

AttributeError is raised when you access an attribute or method that doesn’t exist on the object. Most common cause: calling a method on None (NoneType has no attribute X). Other causes: typo in method name, wrong object type (str when you expected list), or using a feature removed in a newer library version. The error names exactly which type and which missing attribute.

How do I fix ‘NoneType object has no attribute’?

The variable you’re accessing is None, but you expected an object. Trace back to where it was assigned: a function returning None instead of an object (forgot to return), a database query returning no rows (Model.objects.first() returns None when empty), or an API call that failed silently. Safe pattern: if obj is not None: obj.method() OR use the walrus operator: if (obj := get_obj()): obj.method().

How do I check if an attribute exists before accessing it?

Use hasattr(obj, ‘attr_name’) for runtime check, or getattr(obj, ‘attr_name’, default) to get-with-default. For frequent attribute checks, consider type hints + mypy/pyright which catch most AttributeErrors at static-analysis time before runtime.

How do I prevent AttributeError from None values?

Three patterns: (1) Always validate function returns (if result is None: raise). (2) Use type hints with Optional[X] to make None-ability explicit. (3) Use the walrus operator + early return: if (val := get_val()) is None: return default; use val. Defensive coding around None-able returns prevents 90% of AttributeError in production.

Where can I find more AttributeError fixes?

Browse the AttributeError reference hub for 170+ specific fixes (NoneType, pandas, NumPy, sklearn, Selenium). For related errors see TypeError. For Python debugging fundamentals see Python Tutorial hub.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →

Leave a Comment