1

Currently I'm working on this piece of code. It works fine now.

numbers = [1,2,3,4,5,6]
squared_numbers = map(lambda n: n*n, numbers)
# print(list(squared_numbers))
total = reduce(lambda acc, n: n+acc, squared_numbers)

but if I uncomment the print statement it gives me the following error. However if I print squared_numbers the output is still a map object <map object at 0x7f82922750d0>.

total = reduce(lambda acc, n: n+acc, squared_numbers)
TypeError: reduce() of empty sequence with no initial value

Can any one please explain why this is? thanks in advance..

  • 1
    Take a look at https://stackoverflow.com/questions/45017937/python-casting-map-object-to-list-makes-map-object-empty – Chris Dec 07 '20 at 05:58
  • [map](https://docs.python.org/3/library/functions.html#map) returns an iterator. Knowing that perhaps [this](https://stackoverflow.com/questions/9884132/what-exactly-are-iterator-iterable-and-iteration) will answer your question as to why you get an error. – ssp Dec 07 '20 at 06:02

0 Answers0