-2

I've got a list of string elements and I'd like to remove 3 values which are 'English', 'english', and 'French'.

I've tried the following code but the operation doesn't work:

x = ['English','english','French','Dutch','Spanish','Japenese','Italian',]
list = np.random.choice(x,100)
y = [elem for elem in list if elem !='English' or elem !='english' or elem !='French'] 

The final result that I'd like to will be a list without the string values 'English', 'english', and 'French'.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Antoine F
  • 148
  • 8
  • 1
    ˋorˋ means *all* conditions must be false for the entire condition to be false. There is no string for which this is satisfied - e.g. „English“ will trigger the first condition, but both of the others accept it. – MisterMiyagi Jan 19 '20 at 21:19
  • 1
    Related: [Multiple 'or' condition in Python](https://stackoverflow.com/q/22304500/4518341) – wjandrea Jan 19 '20 at 21:21
  • What does `np.random.choice` have to do with the problem? – wjandrea Jan 19 '20 at 21:22

3 Answers3

2

Given elem = 'English', elem !='English' or elem !='english' is True because although elem !='English' is False, elem !='english' is True. That's how or works.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89
1

You want and, not or.

>>> x = ['English', 'english', 'French', 'Dutch', 'Spanish', 'Japenese', 'Italian']
>>> [i for i in x if i != 'English' and i != 'english' and i != 'French']
['Dutch', 'Spanish', 'Japenese', 'Italian']

That said, you could do this more easily with not in:

>>> [i for i in x if i not in ['English', 'english', 'French']]
['Dutch', 'Spanish', 'Japenese', 'Italian']
wjandrea
  • 28,235
  • 9
  • 60
  • 81
0

Here's an option... Just

def remove(elm,lst):
    idxs = []
    for n,k in enumerate(lst):
        if(k == elm):
            idxs.append(n)
    for n,k in enumerate(idxs):
        del(lst[k-n])

x = ['English','english','French','Dutch','Spanish','Japenese','Italian',]
for k in ["english","English","French"]:
    remove(k,x)
print(x) # ['Dutch', 'Spanish', 'Japenese', 'Italian']
kpie
  • 9,588
  • 5
  • 28
  • 50