I have a function that returns a list of the total counts. I want to use the map but don’t know how I can use it.
Here is my function:
JavaScript
x
6
1
def get_bought_passenger_count(self):
2
c = 0
3
for book in self.bookings.all():
4
c += book.passenger_count
5
return c
6
I want to use this with the map.
Advertisement
Answer
I don’t think it’s neccessary.
you are just constantly adding terms, so it’s just a sum
and it doesn’t require to use the map()
function, you could use the builtin sum()
function, and a generator:
JavaScript
1
3
1
def get_bought_passengers(self):
2
return sum(book.passenger_count for book in self.bookings.all())
3
as @Lecdi said in the comments.