10

I have two long lists. I basically want to remove elements from this list that do not match a condtion. For example,

list_1=['a', 'b', 'c', 'd']

list_2=['1', 'e', '1', 'e']

List one and two correspond to each other. Now I would like to remove certain elements from list one that do not match my condition. I have to make sure that I remove the corresponding elements from list 2 and the order does not mess up.

So I created a for loop that goes through list 1 and stores all the indices of elements that have to be removed.

Let’s say:

index_list = ['1', '3']

Basically, I need to make sure I remove b and d from list 1 and e and e from list 2. How do I do this?

I tried:

del (list_1 [i] for i in index_list)]

del (list_2 [i] for i in index_list)]

But I get an error that indices has to be a list, not list. I also tried:

list_1.remove[i]

list_2.remove[i]

But this does not work either. I tried creating another loop:

for e, in (list_1):

    for i, in (index_list):

        if e == i:

            del list_1(i)

for j, in (list_2):

    for i, in (index_list):

        if j == i:

            del list_2(i)

But this does not work either. It gives me an error that e and j are not global names.

shibli049
  • 528
  • 12
  • 31
san3567
  • 119
  • 1
  • 6

4 Answers4

4

try this:

>>> list_1=['a', 'b', 'c', 'd']
>>> list_2 = ['1', 'e', '1', 'e']
>>> index_list = ['1', '3']
>>> index_list = [int(i) for i in index_list] # convert str to int for index
>>> list_1 = [i for n, i in enumerate(list_1) if n not in index_list]
>>> list_2 = [i for n, i in enumerate(list_2) if n not in index_list]
>>> list_1
['a', 'c']
>>> list_2
['1', '1']
>>> 
Fujiao Liu
  • 2,195
  • 2
  • 24
  • 28
1

How about:

list_1, list_2 = zip(*((x, y) for x, y in zip(list_1, list_2) if f(x)))

Where f is a function that tests whether a certain value in list_1 matches your condition.

For example:

list_1 = ['a', 'b', 'c', 'd']
list_2 = ['1', 'e', '1', 'e']


def f(s):
    return s == 'b' or s == 'c'

list_1, list_2 = zip(*((x, y) for x, y in zip(list_1, list_2) if f(x)))

print list_1
print list_2

('b', 'c')

('e', '1')

(Note that this method actually makes list1 and list2 into tuples, which may or may not be okay for your use case. If you really need them to be lists, then you can very easily convert them to lists with the line:

list_1, list_2 = list(list_1), list(list_2)

right after the "primary" line.)

pzp
  • 6,249
  • 1
  • 26
  • 38
0

You can try this:

index_list.sort(reverse=True, key=int)
for i in index_list:
    del(list_1[int(i)])
    del(list_2[int(i)])

Or you also can do this:

list_1 = [item for item in list_1 if f(item)]
list_2 = [item for item in list_2 if f(item)]

Where f is a function which returns True/False depending on your criteria. Like in your example this can be, def f(item): return item != 'a' and item != 'c' and item != 'e'

shibli049
  • 528
  • 12
  • 31
0

A little late to the party, But here is another version.

list_1=['a', 'b', 'c', 'd']

list_2=['1', 'e', '1', 'e']
index_list = ['1', '3']


#convert index_list to int
index_list = [ int(x) for x in index_list ]

#Delete elements as per index_list from list_1
new_list_1 = [i for i in list_1 if list_1.index(i) not in index_list]

#Delete elements as per index_list from list_2
new_list_2 = [i for i in list_2 if list_2.index(i) not in index_list]

print "new_list_1=", new_list_1
print "new_list_2=", new_list_2

Output

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
new_list_1= ['a', 'c']
new_list_2= ['1', '1']
>>> 
Anil_M
  • 10,893
  • 6
  • 47
  • 74