Help adding object properties with different amounts.

Hi i need help getting a python syntax right. I am trying to get all the objects in my scene with property[“books”] in my scene and add them together. This is what I got

objects_with_books = [s for s in scene.objects if ‘books’ in s]

which returns a list. Id like to add them togehter to get a total amount of books. I know how to ad them together if I know how many objects are on scene.

total_books = objects_with_books[0][“books”] + objects_with_books[1][“books”] etc.

My problem is that there are not always and always the same amount of objects with books in the scene. I dont know how to make the python code to iterate by itself and get the total of books regardless of how many objects with books are on the screen. Any help is appreciated.

Using list comprehension:

bookCount = sum([obj['books'] for obj in scene.objects if 'books' in obj])

Or if you prefer the more explicit for loop:

bookCount = 0
for obj in scene.objects if 'books' in obj:
    bookCount += obj['books']

EDIT: Sorry I misread what you were trying to do. Mobious’s post is enough :slight_smile:

Thanks mobius, thats what im was looking for

EDIT: nvm you caught it :smiley: