Josh Rosenberg <[email protected]> added the comment:
Note that all of Serhiy's examples are for a known, fixed number of things to
concatenate/union/merge. str.join's API can be used for that by wrapping the
arguments in an anonymous tuple/list, but it's more naturally for a variable
number of things, and the unpacking generalizations haven't reached the point
where:
[*seq for seq in allsequences]
is allowed.
list(itertools.chain.from_iterable(allsequences))
handles that just fine, but I could definitely see it being convenient to be
able to do:
[].join(allsequences)
That said, a big reason str provides .join is because it's not uncommon to want
to join strings with a repeated separator, e.g.:
# For not-really-csv-but-people-do-it-anyway
','.join(row_strings)
# Separate words with spaces
' '.join(words)
# Separate lines with newlines
'\n'.join(lines)
I'm not seeing even one motivating use case for list.join/tuple.join that would
actually join on a non-empty list or tuple ([None, 'STOP', None] being rather
contrived). If that's not needed, it might make more sense to do this with an
alternate constructor (a classmethod), e.g.:
list.concat(allsequences)
which would avoid the cost of creating an otherwise unused empty list (the
empty tuple is a singleton, so no cost is avoided there). It would also work
equally well with both tuple and list (where making list.extend take varargs
wouldn't help tuple, though it's a perfectly worthy idea on its own).
Personally, I don't find using itertools.chain (or its from_iterable alternate
constructor) all that problematic (though I almost always import it with from
itertools import chain to reduce the verbosity, especially when using
chain.from_iterable). I think promoting itertools more is a good idea; right
now, the notes on concatenation for sequence types mention str.join,
bytes.join, and replacing tuple concatenation with a list that you call extend
on, but doesn't mention itertools.chain at all, which seems like a failure to
make the best solution the discoverable/obvious solution.
----------
nosy: +josh.r
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue33214>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com