Message352387
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. |
|
Date |
User |
Action |
Args |
2019-09-13 18:35:07 | josh.r | set | recipients:
+ josh.r, christian.heimes, eric.araujo, serhiy.storchaka, Javier Dehesa |
2019-09-13 18:35:07 | josh.r | set | messageid: <1568399707.75.0.577639475261.issue33214@roundup.psfhosted.org> |
2019-09-13 18:35:07 | josh.r | link | issue33214 messages |
2019-09-13 18:35:07 | josh.r | create | |
|