Deleting Multiple Elements from a List

Deleting multiple elements from a list

You can use enumerate and remove the values whose index matches the indices you want to remove:

indices = 0, 2
somelist = [i for j, i in enumerate(somelist) if j not in indices]

How to simultaneously delete multiple values from a list in Python based on their index?

try this

a = [e for i, e in enumerate(a) if i not in index_list]

How to remove multiple elements from a list of lists?

You can do it simply with one line using list comprehension:

trackers = [[1, 2, 3], [4,5,6], [7, 8,9], [2,54,23], [4,12,5], [3,2,6]]
filtered = [x for x in trackers if 4 not in x]
print(filtered)

Output:

[[1, 2, 3], [7, 8,9], [2,54,23], [3,2,6]]

How to remove multiple items from a list in just one statement?

In Python, creating a new object e.g. with a list comprehension is often better than modifying an existing one:

item_list = ['item', 5, 'foo', 3.14, True]
item_list = [e for e in item_list if e not in ('item', 5)]

... which is equivalent to:

item_list = ['item', 5, 'foo', 3.14, True]
new_list = []
for e in item_list:
if e not in ('item', 5):
new_list.append(e)
item_list = new_list

In case of a big list of filtered out values (here, ('item', 5) is a small set of elements), using a set is faster as the in operation is O(1) time complexity on average. It's also a good idea to build the iterable you're removing first, so that you're not creating it on every iteration of the list comprehension:

unwanted = {'item', 5}
item_list = [e for e in item_list if e not in unwanted]

A bloom filter is also a good solution if memory is not cheap.

Delete multiple elements from a list

I think you are looking for this

# Example with static values
iex> [1, 2, 3, 4, 5] -- [1, 2]
[3, 4, 5]

# Example with Enum.drop/2, with multiple values
iex> Enum.drop([1, 2, 3, 4, 5], 2)
[3, 4, 5]
iex> Enum.drop([1, 2, 3, 4, 5], -2)
[1, 2, 3]
iex> Enum.drop([1, 2, 3, 4, 5], 20)
[]
iex> Enum.drop([1, 2, 3, 4, 5], -20)
[]

You can use List.delete/2 only can delete one at time and Enum.drop/2 can delete multiple depend how you use it.

Delete multiple elements in a list by index in Python

Assuming you have any old_list with a list of index pos that you want to get rid of:

new_list = [old_list[i] for i, e in enumerate(old_list) if i not in pos]

This will work for both the list in your question by getting rid of the element at index specified by pos, just substitute old_list with list name you currently have:

dS = [0, 0.02, 0, 0.04, 0.07, 0]
dN = [1, 0.02, 0.3, 0.7, 0.9]
pos = [i for i, e in enumerate(dS) if e ==0]
dS = [dS[i] for i, e in enumerate(dS) if i not in pos]
dN = [dN[i] for i, e in enumerate(dN) if i not in pos]
>>> dS, dN
([0.02, 0.04, 0.07], [0.02, 0.7, 0.9])

This work fine for list which lengths are different, as shown in your case above.

More succinct way to remove multiple elements from a list?

I am not sure if this is what you have in mind, but usually regular expressions are useful for extracting patterns from strings. For example:

import re
my_list = ['from ab1c_table in WXY\nprevious in time',
'from abc3_table in MNO\nprevious in time']

my_list1 = [re.findall(r" ([A-Z]{3})\n", s, )[0] for s in my_list]
print(my_list_1)

Edit:

Here is a modification of the regex pattern reflecting the additonal string samples provided by OP in a comment below:

mylist = ['from ab1c_table in WXY\nprevious in time', 
'from abc3_table in MNO\nprevious in time',
'from ab1_cow_table in DZMC1_IN tab\ncurrent in time',
'from abc4_table in ERDU\ncurrent in time']

my_list1 = [re.findall(r"_table in (\S+)(?:| tab)\n.* in time", s)[0] for s in mylist]

print(my_list1)

This gives:

['WXY', 'MNO', 'DZMC1_IN', 'ERDU']

Edit 2:

Version capturing _table patterns:

import re
from itertools import chain

mylist = ['from ab1c_table in WXY\nprevious in time',
'from abc3_table in MNO\nprevious in time',
'from ab1_cow_table in DZMC1_IN tab\ncurrent in time',
'from abc4_table in ERDU\ncurrent in time']

my_list1 = list(chain(*[re.findall(r"from (\S+_table) in (\S+).*?\n.* in time", s)[0] for s in mylist]))

print(my_list1)

It gives:

['ab1c_table', 'WXY', 'abc3_table', 'MNO', 'ab1_cow_table', 'DZMC1_IN', 'abc4_table', 'ERDU']

Remove multiple list elements knowing their indices in python

You can use enumerate to iterate through your list, with corresponding indices. Then check those indices against your removal list.

>>> [val for idx, val in enumerate(my_list) if idx not in my_removal_indices]
['one', 'three']

If the index list was long, for performance could also switch to a set to speed up the in check

my_removal_indices = {0, 2, 4}
>>> [val for idx, val in enumerate(my_list) if idx not in my_removal_indices]
['one', 'three']

How to move multiple elements from one list to another and delete from first list

Generally speaking, you shouldn't try to modify a list as you're iterating over it, as the memory is shifting while you're trying to access it (the mapping between the elements in list_1 and to_move may not be easily retained if you remove elements from list_1 as well).

Instead, use a list comprehension:

list_1 = [1, 2, 3, 4]
to_move = [True, False, False, True]

list_2 = [elem for index, elem in enumerate(list_1) if to_move[index]]
list_1 = [elem for index, elem in enumerate(list_1) if not to_move[index]]

print(list_1, list_2) # Prints [2, 3] [1, 4]


Related Topics



Leave a reply



Submit