AttributeError: 'tuple' object has no attribute 'append'

Hello I’m working on the Frida Kahlo off platform project i’m stuck on task number four and keep getting this error. I’m not sure what i’m doing wrong in order to get this message. I have tried to fix it and look it up, any help would be great! Here’s my code…

paintings = ‘The Two Fridas’, ‘My Dress Hangs Here’, ‘Tree of Hope’, ‘Self Portrait With Monkeys’

dates = 1939, 1933, 1946, 1940

#Zipping paintings and dates into an object.

paintings = list(zip(paintings,dates))

print(paintings)

Adding new paintings to the collection. Adding names of paintings onto the paintings list.

new painting entry

paintings.append(‘The Broken Column’)
dates.append(1944)

#######

Here’s is the error I keep getting AttributeError: ‘tuple’ object has no attribute ‘append’
PLEASE HELP! thanks

The error message explains it: a tuple sequence cannot use a list method. Tuples are not lists and as such, cannot use methods that are specific to a list. More importantly, tuples are immutable (lists are mutable) so you can’t add or remove items from a tuple.

Are paintings and dates lists? Because the unformatted code you pasted doesn’t show they are lists.
Also, why are you adding 1944 to dates, rather than the zipped list paintings? Is dates a tuple? if so, that’s why you’re getting the error. Isn’t the code supposed to be:

paintings.append(('The Broken Column', 1944)) ?

Alternatively,
To not get tuples as a result, you could use a list comprehension (not sure if that’s been covered yet or not):

paintings = [list(x) for x in zip(paintings, dates)]
print(paintings)
>>[['The Two Fridas', 1939], ['My Dress Hangs Here', 1933], ['Tree of Hope', 1946], ['Self Portrait With Monkeys', 1940]]

Some more info on zip(). zip() creates an iterator of tuples, which is why you have to cast it as a list()

See lists:

And tuples,

#immutable
a = ('jerry', 'george', 'elaine', 'kramer')
a.append('larry')
>>AttributeError Traceback (most recent call last)
<ipython-input-73-2f0cd209f635> in <cell line: 2>()
      1 a = ('jerry', 'george', 'elaine', 'kramer')
----> 2 a.append('larry')
      3 

AttributeError: 'tuple' object has no attribute 'append'

vs:

#mutable
b = ['buffy', 'willow', 'xander', 'giles']
b.append('spike')
print(b)
>>['buffy', 'willow', 'xander', 'giles', 'spike']
3 Likes

Whenever we are in doubt as to what methods (attributes) an object has, there is always the old,

>>> x = (1, 2, 3, 4)
>>> hasattr(x, 'append')
False
>>> y = [*x]    # same as `y = list(x)`
>>> hasattr(y, 'append')
True
>>> 

Or we can list all the attributes,

>>> dir(x)
    [ ... ]    # 'append' is not among them
>>> dir(y)
    [ ... ]    # 'append' IS among them.

FTR, Python permits this type of sequence which it casts to type, <class 'tuple'>, automatically.

>>> dates = 1939, 1933, 1946, 1940
>>> type(dates)
    <class 'tuple'>
>>> 
3 Likes