-->
With Examples Fix: AttributeError: 'list' object has no attribute' patches' and AttributeError: 'list' object has no attribute 'apply' Python

With Examples Fix: AttributeError: 'list' object has no attribute' patches' and AttributeError: 'list' object has no attribute 'apply' Python

Back to top

Posted by Ashirafu Kibalama on September 26, 2024

Fix and Resolve AttributeError: 'list' object has no attribute' patches' and 'apply' Python






The AttributeError: 'list' object has no attribute' patches' and AttributeError: 'list' object has no attribute 'apply' Python occurs when accessing either the patches method or apply method from a list object that doesn't have such an attribute.


Fix AttributeError: 'list' object has no attribute 'patches' Python With Examples.




The AttributeError: 'list' object has no attribute' patches' Python usually occurs when accessing the patches attribute from a list object that doesn't have such an attribute.


Patches refers to a class of objects that represents various geometric shapes used in visualizations in the Matplotlib library in Python.

Let's review three examples of this error and explain how to fix each.


3 Examples of AttributeError: 'list' object has no attribute 'patches' Python and how to fix each:


Example 1: Accessing patches on Multiple Axes Objects in a List.

The axes_list contains two Axes objects (ax1 and ax2), and you cannot directly access patches on the list of Axes.



import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(1, 2)
axes_list = [ax1, ax2]
print(axes_list.patches) # Raises AttributeError: 'list' object has no attribute 'patches'


Output:




Solution.

Loop over the list to access the patches attribute for each Axes object.


import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(1, 2)
axes_list = [ax1, ax2]

for ax in axes_list:
print(ax.patches) # Correct way to access patches


Output:




Example 2: Accessing patches on a List of Dictionaries.

list_of_dicts is a list of dictionaries, but you're trying to access the patches attribute directly on the list instead of individual dictionaries.



# List of dictionaries, each representing some kind of object
list_of_dicts = [{'patches': [1, 2]}, {'patches': [3, 4]}]
print(list_of_dicts.patches) # Raises AttributeError: 'list' object has no attribute 'patches'


Output:




Solution.

You should access patches by iterating through the list of dictionaries.



# List of dictionaries, each with a 'patches' key
list_of_dicts = [{'patches': [1, 2]}, {'patches': [3, 4]}]

# Access the 'patches' key from each dictionary in the list
for d in list_of_dicts:
print(d['patches']) # Correct way to access patches from each dictionary


Output:





Example 3: Accessing patches on a List of Named Tuples.

tuple_list is a list of named tuples, but you're trying to access patches on the list itself, which doesn't exist.



from collections import namedtuple

# Define a named tuple with a 'patches' attribute
MyTuple = namedtuple('MyTuple', ['patches'])

# Create a list of named tuples
tuple_list = [MyTuple(patches=[1, 2]), MyTuple(patches=[3, 4])]

# Incorrectly trying to access patches on the list
print(tuple_list.patches) # Raises AttributeError: 'list' object has no attribute 'patches'


Output:




Solution.

You need to loop through the list and access the patches attribute for each named tuple.



from collections import namedtuple

# Define a named tuple with a 'patches' attribute
MyTuple = namedtuple('MyTuple', ['patches'])

# Create a list of named tuples
tuple_list = [MyTuple(patches=[1, 2]), MyTuple(patches=[3, 4])]

# Loop through the list and access the 'patches' attribute for each named tuple
for item in tuple_list:
print(item.patches) # Correct way to access patches from each named tuple


Output:





Fix AttributeError: 'list' object has no attribute 'apply' Python With Examples.



This AttributeError: 'list' object has no attribute 'apply' Python occurs when using a method that belongs to another object type (like DataFrame or Series in Pandas) on a Python list. Lists in Python don't have an apply() method specific to Pandas.

Let's review three examples of this error and explain how to fix each.


3 Examples of AttributeError: 'list' object has no attribute 'apply' Python and how to fix each:


Example 1: Using a Function to Each Element in a List.

You may apply a function to each list element using apply(), which is available for Pandas objects but not for Python lists.


my_list = [1, 2, 3, 4]
result = my_list.apply(lambda x: x * 2) # This will raise the AttributeError


Output:




Solution.

Use list comprehension or the built-in map() function to fix this.

With List Comprehension:



my_list = [1, 2, 3, 4]
result = [x * 2 for x in my_list]
print(result)


Output:




With map():


my_list = [1, 2, 3, 4]
result = list(map(lambda x: x * 2, my_list))
print(result)


Output:





Example 2: Using apply() with Pandas DataFrames.

You might confuse a list of values with a Pandas DataFrame or Series that uses the apply() method. The apply() method applies a function along an axis of a data frame or on elements of a Series.


import pandas as pd

my_list = [1, 2, 3, 4]
df = pd.DataFrame(my_list)
result = my_list.apply(lambda x: x * 2) # This will raise the AttributeError


Output:




Solution.

Ensure you apply the apply() method on a Pandas DataFrame or Series object.



import pandas as pd

df = pd.DataFrame([1, 2, 3, 4])
result = df.apply(lambda x: x * 2)
print(result)


Output:




Alternatively, convert your list to a Pandas Series if you want to use apply().



import pandas as pd

my_list = [1, 2, 3, 4]
series = pd.Series(my_list)
result = series.apply(lambda x: x * 2)
print(result)


Output:




Example 3: Confusing apply() with append() for Lists.

Many providers confuse the apply() method with append(), a valid list method. The apply() method doesn't exist for lists, but append() does for adding elements.



my_list = [1, 2, 3, 4]
my_list.apply(5) # This will raise the AttributeError


Output:




Solution.

Use the append() to add an element to a list.



my_list = [1, 2, 3, 4]
my_list.append(5)
print(my_list)


Output:





Conclusion.

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

  • Ensure you're not accessing patches on a list directly.
  • Understand the Object Type: The patches attribute belongs to the objects in the list, not the list itself.
  • Iterate Over the List: Access patches from each element within the list.


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

  • Use list comprehension or map() to transform lists.
  • Ensure you use the apply() method on Pandas DataFrames or Series.
  • Use append() when you need to add elements to a list.


Using the above approaches, you can avoid the AttributeError: 'list' object has no attribute 'apply' and AttributeError: 'list' object has no attribute 'patches' Python errors.