4

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 ?

1
  • Don't use sum to flatten a list of lists. It's highly inefficient. Commented Jul 5, 2021 at 3:55

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Apart from all the intermediate lists generated internally by sum!
... ... ... Yeah, that's fair. I didn't think about that xD
You should really not use sum here
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.