Skip to content Skip to sidebar Skip to footer

Join With A Separator Added At The End When Not Empty

I need to append all elements in list_ to a string; at the end I need to add a suffix. A dot '.' has to separate all elements: list_ = args[f(n) : f(n+1)] if list_: string += '.'

Solution 1:

I would go with this (updated to reflect edits to the question):

'.'.join([''] + args[f(n):f(n+1)] + [suffix])

EDIT: taking inspiration from sblom's and unutbu's answers, I might actually do this:

from itertools import chain, islice
string = '.'.join(chain([string],  islice(args, f(n), f(n+1)), [suffix]))

if I were concerned about the memory cost of slicing args. You can use a tuple (string,) or a list [string] for the string and the suffix; given that they're one element each, there's no significant difference in memory usage or execution time, and since they're not being stored, you don't have to worry about mutability. I find the list syntax a little cleaner.

However: I'm not sure if Python actually creates a new list object for a slice that is only going to be used, not assigned to. If it doesn't, then given that args is a proper list, using islice over [f(n):f(n+1)]doesn't save you much of anything, and in that case I'd just go with the simple approach (up top). If args were a generator or other lazily evaluated iterable with a very large number of elements, thenislice might be worth it.

Solution 2:

If list_ is a generator (or can be trivially changed to be one), you can get away without materializing any lists at all using chain() from itertools.

from itertools import chain
'.'.join(chain(('',),list_,(suffix,)))

(This takes inspiration from David Zaslavsky's answer.)

Solution 3:

Also riffing on David Zaslavsky answer:

string = '.'.join([string] + list_ + [suffix])

The advantage of doing it this way is that there is no addition of strings.

Solution 4:

string += ('.' + '.'.join(list_) if list_ else'') + '.' + suffix 

Post a Comment for "Join With A Separator Added At The End When Not Empty"