How do you calculate multiple objects values in a list

Alan Huang

I'm trying to add the sum of objects from a database but so far I have found no success

test = [{'shares': 5}, {'shares': 1}]
# print sum of the two shares (5+1=6)

I've tried to use test['shares'] to print but it results in the error:

list indices must be integers or slices, not str

I've tried sum(test.value())but creates the error

'list' object has no attribute 'values'

And sum(test)creates:

unsupported operand type(s) for +: 'int' and 'dict'

It seems like the objects are wrapped in a list but I do not know how to unwrap it?

Zev Chonoles

One nice way would be

sum(x['shares'] for x in test)

Why does this work?

Given a dictionary d = {'shares': 7}, then to get the value 7, you call d['shares'].

You want to do that for each dictionary in the list test, so you want to look at x['shares'] for each x in test.

In Python, the function sum can accept something called a "generator expression", which is what we did here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you Marshal multiple sets of objects with different values in JAXB?

How do you calculate data completeness for multiple tables based on null values within columns?

How do you store a list of objects in an array?

How do you convert a list of json objects to a list of javascript objects?

How do you substitute the values of sympy.core.add.add objects when they are in a list?

How do you return identical elements based on a single attribute from a list of objects with multiple attributes?

How do you addFacetRefinement with multiple values in Algolia?

How do you calculate a single Median for multiple columns in a dataframe?

How do you calculate accuracy metric for a regression problem with multiple outputs?

How do you calculate the distribution of frequencies in a list in Python?

How do you calculate the sum of a specific row in a list of matrices

How to iterate over multiple list and calculate every alternate values in it?

How do you return multiple function values to multiple global variables?

How do you pass multiple objects to go template?

Java - How do you avoid a StackOverflow after creating multiple objects?

Calculate sum of multiple values in array of objects

How do i calculate the percentage increase or decrease in a list of values with python

How do you change parts of a string in each element of a list of objects?

How do you remove trailing objects from a list with linq?

How do you create a list of objects to pass to a widget in Flutter?

How do you pass a nullable list of objects with Parcelable

How do you create a table from a list of objects using React?

In Terraform, how do you output a list from an array of objects?

In terraform, how do you sort a list of objects and grab the last value?

How do you sum on a list of objects in a java 8 stream

How do you loop through a list of objects and get a specific attribute?

setState for multiple values in objects list

How do you accept multiple values for a switch case?

how do you quote multiple values with the helm quote function?