I am having elements inside a list from where I want to remove all elements except the certain index values, is there a way?
list = ['Radar', 'completed 2022-10-23T08:18:26', 'PASS: 11FAIL: 0SKIP: 0', '0:14:55', 'completed', '2022-10-23T08:18:26']
indexes = (0, 1, -2, -1) # these are the index values i want to keep in the same list sorted in same indexes format
expected,
list = ['Radar', 'completed 2022-10-23T08:18:26', 'completed', '2022-10-23T08:18:26']
CodePudding user response:
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.
CodePudding user response:
In one line:
list = [list[i] for i in [1, 2, -1, -2]]
Hope it helps!
CodePudding user response:
I think it's simpler and probably faster to create a new list with something like:
list = list[i for i in range(len(list)) if i in indices_to_keep]
That said, sometimes you need to modify a list passed as an argument, or that may have other bindings that you can't find or update. You can use del
statements to remove indices you don't want, but each delete will change the indices of all elements following the deleted index, so you need to take a bit of care. One simple way is to delete them in reverse order with something like:
for i in reversed(range(len(list))):
if i not in keep_indices: del list[i]
ETA: I took some care to preserve the original order of the non-deleted elements. If that's not a requirement then the other answers will probably work better for you. Also, I see you're using negative indices. For those to work with the code above, you could normalize them with something like indices_to_keep = [x%len(list) for x in indices_to_keep]
.
CodePudding user response:
The simplest is the list comprehensions suggested in other answers, but if you use the pandas library, you can do
my_series = pd.Series(my_list)
my_series.loc[indexes]
If you're doing this a lot, or are using pandas elsewhere, it might be worth the pandas overhead.