Single line python

I want a one line solution In Python the following code, but how?

total = 0 for ob in self.oblist: total+=sum(v.amount for v in ob.anoutherob) 

Returns the total value. I want one liner, plz help me

+4
source share
2 answers

No need to double sum() calls

 total = sum(v.amount for ob in self.oblist for v in ob.anotherob) 
+24
source

You can simply roll the for loop to another level of understanding:

 total = sum(sum(v.amount for v in ob.anotherob) for ob in self.oblist) 
+5
source

All Articles