-->
With Examples Fix: AttributeError: 'list' object has no attribute' title' and AttributeError: 'list' object has no attribute 'keys' Python.

With Examples Fix: AttributeError: 'list' object has no attribute' title' and AttributeError: 'list' object has no attribute 'keys' Python.

Back to top

Posted by Ashirafu Kibalama on September 25, 2024

Fix and Resolve AttributeError: 'list' object has no attribute' title' and 'keys' Python.






The errors AttributeError: 'list' object has no attribute' title' and AttributeError: 'list' object has no attribute 'keys' Python occurs when you use either the .title() string method or the .keys() method on a list object.


Fix AttributeError: 'list' object has no attribute 'title' Python with Examples.





The AttributeError: 'list' object has no attribute' title' Python occurs when you attempt to use the .title() string method on a list object. A list, a collection of items, doesn't have the .title() method used to capitalize the first letter of each word in a string.

Below are three scenarios where this error may occur and solutions to fix each case.


3 Examples of AttributeError: 'list' object has no attribute' title' Python and solutions to fix each case.      


Example 1: Direct Application of .title() on a List.

You're trying to use .title() on an entire list rather than individual strings within the list.



fruits = ['apple', 'banana', 'cherry']
print(fruits.title()) # raise AttributeError: 'list' object has no attribute 'title'


Output:




The .title() method is for strings, not lists. Since fruits are a list, Python raises the AttributeError.


Solution.

Use a list comprehension to apply .title() to each list element.



fruits = ['apple', 'banana', 'cherry']
title_fruits = [fruit.title() for fruit in fruits]
print(title_fruits)


Output:




Example 2: Attempting to Call .title() on a List Stored in a Dictionary.

You may be trying to apply .title() to a list that's a value in a dictionary.



data = {'names': ['john', 'jane', 'doe']}
print(data['names'].title()) # raise AttributeError: 'list' object has no attribute 'title'


Output:




Data ['names'] is a list, so attempting to apply .title() directly on it leads to the error.


Solution.

Apply .title() to each string in the list, similar to Example 1.


data = {'names': ['john', 'jane', 'doe']}
data['names'] = [name.title() for name in data['names']]
print(data)


Output:




Example 3: Nested Lists and Incorrect Use of .title().

You may have a list of lists (nested list), and you accidentally try to apply .title() to the entire nested structure.



nested_list = [['apple', 'banana'], ['cherry', 'date']]
print(nested_list.title()) # Error: 'list' object has no attribute 'title'


Output:




The nested_list contains lists inside of it, and trying to apply .title() to the entire structure raises the error.


Solution.

To apply .title () to each string, you must loop over the outer and inner lists.


nested_list = [['apple', 'banana'], ['cherry', 'date']]
title_nested_list = [[item.title() for item in sublist] for sublist in nested_list]
print(title_nested_list)


Output:





Fix AttributeError: 'list' object has no attribute 'keys' Python with Examples.




In Python, AttributeError: 'list' object has no attribute 'keys' typically occurs when you attempt to call the .keys() method on a list object, which is invalid because lists do not have keys (the .keys() method is for dictionaries).


Here are three examples of the AttributeError: 'list' object has no attribute 'keys' in Python, along with explanations and fixes.


3 Examples of AttributeError: 'list' object has no attribute' keys' Python and solutions to fix each case.      


Example 1: Trying to call .keys() on a list of dictionaries.

Here, data is a list of dictionaries, and the error occurs because .keys() is a method for dictionaries, not lists.



data = [{'name': 'John', 'age': 30}, {'name': 'Jane', 'age': 25}]

# Attempting to call keys() on the list itself
keys = data.keys() # Raises AttributeError


Output:




Solution.

To access the keys of each dictionary inside the list, you need to iterate over the list elements.



data = [{'name': 'John', 'age': 30}, {'name': 'Jane', 'age': 25}]

# Access keys of each dictionary in the list
for item in data:
print(item.keys()) # This works, as each item is a dictionary


Output:




Example 2: Misuse of .keys() on a list instead of a dictionary.

The key' users' contains a list of user dictionaries, but you are trying to call .keys() directly on the list.


response = {'users': [{'name': 'John', 'age': 30}, {'name': 'Jane', 'age': 25}]}

# Attempting to access keys from the list of users
keys = response['users'].keys() # Raises AttributeError


Output:




Solution.

Access the keys of the individual dictionaries inside the list.


response = {'users': [{'name': 'John', 'age': 30}, {'name': 'Jane', 'age': 25}]}

# Loop through each dictionary in the list and access its keys
for user in response['users']:
print(user.keys()) # Accesses keys of each user dictionary


Output:




Example 3: Mishandling JSON response (typical in web APIs).

After loading the JSON string, data['users'] is a list, and lists don't have .keys(). The error occurs because you are treating the list as if it were a dictionary.



import json

# Simulating a JSON response from an API
json_data = '{"users": [{"name": "John", "age": 30}, {"name": "Jane", "age": 25}]}'

# Parsing JSON string into Python object
data = json.loads(json_data)

# Trying to access keys from a list instead of the dictionary
keys = data['users'].keys() # Raises AttributeError


Output:




Solution.

Iterate over the list of dictionaries to access their keys.



import json

json_data = '{"users": [{"name": "John", "age": 30}, {"name": "Jane", "age": 25}]}'
data = json.loads(json_data)

# Loop through the list of dictionaries to access keys
for user in data['users']:
print(user.keys()) # Access keys for each user dictionary


Output:






Conclusion.

To fix and resolve AttributeError: 'list' object has no attribute' title' Python:

  • Use a list comprehension to apply .title() to each string in a simple list.
  • If dealing with a list inside a dictionary, apply .title() to each list element.
  • When dealing with nested lists, use nested list comprehensions to apply .title() to each string at all levels.


To fix and resolve AttributeError: 'list' object has no attribute 'keys' Python:

  • Iterate over lists to access the dictionaries inside them.
  • Ensure you're calling .keys() on dictionary objects, not lists.
  • Check your data structure: If the variable is a list, access its elements before calling dictionary methods like .keys().


These fixes ensure you handle each string individually before applying string-specific methods like .title() or .keys().