I have two lines in my program that seems fairly simple but has been given me some headache
here it is
costs = sum([map(int, f.readline().strip().split(' ')) for i in range(8)], [])
awards = sum([map(int, f.readline().strip().split(' ')) for i in range(8)], [])
It gives me the error: TypeError: can only concatenate list (not "map") to list
It seems to me that it something to do with python 2 to python 3 version but I can't figure it out!
I am stuck in this for several hours and cant find a solution. Any help ?
You're correct. In Python 2, map returned a list. In Python 3, it returns a special iterable that lazily maps over the collection. Simply convert this iterable to a list explicitly.
costs = sum((list(map(int, f.readline().strip().split(' '))) for i in range(8)), [])
Note also that by using parentheses rather than brackets on the generator expression, we can avoid producing an intermediate list.
sum([... for i in range(8)])
This produces a list fully in memory and then sums the values.
sum(... for i in range(8))
This iterates over the range and produces a sum as we go, never making an intermediate list.
Instead of summing lists in this fashion you can use chain:
list(chain.from_iterable(
    map(int, f.readline().strip().split(' ')) for i in range(8)))
This will be much more memory efficient, and for larger values of your loop it may become significantly faster as well.
Using sum causes many temporary lists to be made ... if we are using sum(L, []) then internally this is producing many intermediate lists (of lists)
sum(L, []) ->
  tmp = [] + L[0]
  tmp = tmp + L[1]
  ...
  tmp = tmp + L[7]
Every step of this sum, discards the previous tmp while constructing a new one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With