Tuples and Lists

python

#1

I have a list of tuples in the variable tups_list, each tuple of which contains 2 strings and a list. I am trying to create a list of Photo instances, where each Photo instance uses the data from each tuple in the tups_list and save that list in a variable photo_insts.

photo_instances = []
tups_list = [("Portrait 2","Gordon Parks",["chicago", "society"]),("Children in School","Dorothea Lange",["children","school","1930s"]),("Airplanes","Margaret Bourke-White",["war","sky","landscape"])]
for i in tups_list:
        photo_instances = (i[0],i[1],i[2])
        photo_instances.append(photo_instances)
        print i

current error: AttributeError: 'tuple' object has no attribute 'append'....I understand the error but I am not sure how to fix it. Any help would be great.


#2

you have a list:

photo_instances = []

and tuple:

photo_instances = (i[0],i[1],i[2])

which have the same name, i think you need to rename the tuple:

photo_tuple = (i[0],i[1],i[2])
photo_instances.append(photo_tuple)

#3

Great that makes sense!


#4

because you overwrote your list with a tuple, then you try to append a tuple to a tuple (which gives an error message since you can't append to tuples)