It's easier to just replace the list with a new list that only has the needed values:
xs = ['Radar', 'completed 2022-10-23T08:18:26', 'PASS: 11FAIL: 0SKIP: 0', '0:14:55', 'completed', '2022-10-23T08:18:26']
indices = (0, 1, -2, -1)
xs = [xs[i] for i in indices]
print(xs)
By the way, don't call variables names like list
, this 'shadows' the list
type, making it inaccessible to the code that follows your code and causing your code to become confusing.
If, for some reason, you absolutely need a solution that removes the other indices instead of keeping the specified ones:
xs = ['Radar', 'completed 2022-10-23T08:18:26', 'PASS: 11FAIL: 0SKIP: 0', '0:14:55', 'completed', '2022-10-23T08:18:26']
indices = (0, 1, -2, -1)
n = len(xs)
# turn the negative indices into positive ones
indices = tuple(n + i if i < 0 else i for i in indices)
# get the indices that need to be deleted, from the end to the start
del_indices = reversed(sorted(i for i in range(n) if i not in indices))
for i in del_indices:
del xs[i]
print(xs)
This is more complicated because as you remove items, the indices for the remaining items might change, unless you remove them in the correct order. As long as you remove elements starting at the end, going to the start, there won't be an issue.
A smarter solution would remove entire ranges, instead of a single element at a time - but that's more trouble than it's worth and you should probably use the first solution.