0

I am having the list like below

list1=['abc','oops','#exclude=10','exclude=java* kln*','smith','johns']

I am trying to delete the element which contains the word 'exclude' by using the below code.

x=0  

for i in list1:

    if 'exclude' in i:
        list1.pop(x)
    x+=1 

print list1

When I run the program, it is removing the first exclude not the second one. Please let me know how to remove all exclude and what is the mistake I am doing?

Nishant
  • 20,354
  • 18
  • 69
  • 101
iamarunk
  • 109
  • 2
  • 16

5 Answers5

1

Here's a simple solution:

import re
list1=['abc','oops','#exclude=10','exclude=java* kln*','smith','johns']
regex = re.compile('.*exclude.*')
okay_items = [x for x in list1 if not regex.match(x)]
print(okay_items)

In your solution, you have used pop() and as per the documentation,

list.pop([i]):

Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list.

Community
  • 1
  • 1
Harish Talanki
  • 866
  • 13
  • 27
1

The reason you are experiencing this kind of behavior is that you are mutating list1 as you are iterating over it. At the point when you pop #exclude=10 from list1, x == 2 and once the element is popped

     list1 == ['abc','oops','exclude=java* kln*','smith','johns']

Now x increments to 3, but list1[3]==smith after popping, whereas you were expecting it to be exclude=java* kln* as in your original version of list1.

alpacahaircut
  • 337
  • 1
  • 3
  • 11
0

try this,

>>> list1=['abc','oops','#exclude=10','exclude=java* kln*','smith','johns']
>>> [i for i in list1 if 'exclude' not in  i]
['abc', 'oops', 'smith', 'johns']
>>> 

simple method without list comprehension and effect in original list,

>>> list1=['abc','oops','#exclude=10','exclude=java* kln*','smith','johns']
>>> for i in filter(lambda x: 'exclude' in x, list1):
...   list1.remove(i)
... 
>>> list1
['abc', 'oops', 'smith', 'johns']
>>> 
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
  • @Mohammed thanks for your reply. How to write normal code instead of list comprehension. Also how to effect that in the original list – iamarunk Feb 26 '18 at 08:49
  • `>>> list1=['abc','oops','#exclude=10','exclude=java* kln*','smith','johns'] >>> for i in range(len(list1)): ... if 'exclude' in list1[i]: ... print i ... list1.pop(i) ... 2 '#exclude=10' Traceback (most recent call last): File "", line 2, in IndexError: list index out of range >>> ` – Mohideen bin Mohammed Feb 26 '18 at 11:26
  • but you wont do it in original list.. once loop it took initial length of list.. during loop if your value match you have to pop. so it affect len of list . on next loop it affect further – Mohideen bin Mohammed Feb 26 '18 at 11:27
  • my answer has been updated as you want... check and let me know – Mohideen bin Mohammed Feb 26 '18 at 12:03
0

The method pop() removes and returns last object or obj from the list.

Instead you can create a new list which does not include strings with "exclude" like this.

list1=['abc','oops','#exclude=10','exclude=java* kln*','smith','johns']
listWithoutExclude = []
for each in list1:
    if "exclude" not in each:
        listWithoutExclude.append(each)
print listWithoutExclude
Ankush Rathi
  • 622
  • 1
  • 6
  • 26
0

Since while deleting first element list shifting it's element that's why this is happening.

You can try this:-

list1=['abc','oops','#exclude=10','exclude=java* kln*','smith','johns']
new_ls = [list1.index(x) for x in list1 if 'exclude' in x]
for i in reversed(new_ls):
    list1.pop(i)
print(list1)
Narendra
  • 1,511
  • 1
  • 10
  • 20