Python – Improving Python list slicing


I've wondered why extend/append methods of Python don't return a reference to result list.
To build string of all combination of list with last element, I would like to write simple:

for i in range(l, 0, -1):
    yield " ".join(src[0:i-1].append(src[-1]))

But I've got: TypeError. Instead following code with intermediate variable is used:

 for i in range(l, 0, -1):
        sub = src[0:i-1]
        yield " ".join(sub)

Correct me please if I'm wrong

Best Solution

Hm, maybe replace:



src[0:i-1] + src[-1:] #note the trailing ":", we want a list not an element