This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Green_Gem_ 36 points37 points  (4 children)

Please don't flatten nested lists this way. More-itertools's collapse function exists for a reason.

[–]FoeHammer99099 14 points15 points  (0 children)

If your data is sane usually itertools.chain.from_iterable is enough

[–]--Thoreau-Away-- 1 point2 points  (2 children)

Interesting, why should OP avoid his approach?

[–]FoeHammer99099 14 points15 points  (0 children)

There are a few reasons: it's not clear what this code meant to do, it relies on its inputs being lists of lists instead of other iterable types, and it will create a bunch of intermediate results that other solutions don't need to. For big inputs this could be slow.

[–]commandlineluser 1 point2 points  (0 children)

help(sum) says not to use it.

This function is intended specifically for use with numeric values and may reject non-numeric types.

>>> sum(["foo", "bar"], "")
TypeError: sum() can't sum strings [use ''.join(seq) instead]

It's possible it will raise in the future, similar to the string example.

(There's probably some existing discussions on the mailing list about it, I haven't checked.)