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 += '.' + '.'.join(list_) + '.' + suffix # works except when list_ is empty
else:
string += '.' + suffix
# list_ isn't used after this
Can I rewrite it in a simpler way in one line? If join
added a separator after each element, it would just this:
string += '.' + '.'.join(args[f(n) : f(n+1)]) + '.' + suffix
Edit
I just learned that:
Slices are copies even if they are never assigned to: Does Python do slice-by-reference on strings?
But islice may be even worse since it iterates through the start of the list: itertools.islice compared to list slice
Some alternatives are discussed here: Avoiding unnecessary slice copying in Python