62

I want to remove all elements in a list which contains (or does not contain) a set of specific characters, however I'm running in to problems iterating over the list and removing elements as I go along. Two pretty much equal examples of this is given below. As you can see, if two elements which should be removed are directly following each other, the second one does not get removed.

Im sure there are a very easy way to do this in python, so if anyone know it, please help me out - I am currently making a copy of the entire list and iterating over one, and removing elements in the other...Not a good solution I assume

>>> l
['1', '32', '523', '336']
>>> for t in l:
...     for c in t:
...         if c == '2':
...             l.remove(t)
...             break
...             
>>> l
['1', '523', '336']
>>> l = ['1','32','523','336','13525']
>>> for w in l:
...     if '2' in w: l.remove(w)
...     
>>> l
['1', '523', '336']

Figured it out:

>>> l = ['1','32','523','336','13525']
>>> [x for x in l if not '2' in x]
['1', '336']

Would still like to know if there is any way to set the iteration back one set when using for x in l though.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Vidar
  • 4,141
  • 5
  • 24
  • 30

4 Answers4

111

List comprehensions:

l = ['1', '32', '523', '336']

[ x for x in l if "2" not in x ]

# Returns: ['1', '336']

[ x for x in l if "2" in x ]

# Returns: ['32', '523']
l = ['1', '32', '523', '336']
stringVal = "2"

print(f"{[ x for x in l if stringVal not in x ]}")

# Returns: ['1', '336']

print(f"{[ x for x in l if stringVal in x ]}")

# Returns: ['32', '523']
JayRizzo
  • 3,234
  • 3
  • 33
  • 49
MattH
  • 37,273
  • 11
  • 82
  • 84
  • MattH, I'm sorry, is it possible to do so that `if "1" or "2" or "3" not in x`. That is, to be equivalent `[ x for x in l if "1" not in x ]` + `[ x for x in l if "2" not in x ]` + `[ x for x in l if "3" not in x ]`, but shorter? Thanks. – Саша Черных Aug 10 '18 at 16:01
  • @СашаЧерных You're describing `[x for x in l if any(y not in x for y in '123')]`, but I think you actually want `[x for x in l if all(y not in x for y in '123')]` – wjandrea Sep 03 '19 at 13:59
  • Could somebody explain this code? Or write its longer version? – BARIS KURT May 11 '22 at 09:20
  • @СашаЧерных is there a way to exclude not just one value, but a list containing specific elements say, exclude_list = ['3', '5', '6']. Now all elements of list "l" containing these strings should be excluded. How would can we achieve this? – EnigmAI Jul 05 '22 at 14:12
12

If I understand you correctly,

Example:

l = ['1', '32', '523', '336']

[x for x in l if "2" not in x]

# Returns: ['1', '336']

fString Example:

l = ['1', '32', '523', '336']

stringVal = "2"

print(f"{[x for x in l if stringVal not in x]}")

# Returns: ['1', '336']

might do the job.

JayRizzo
  • 3,234
  • 3
  • 33
  • 49
loevborg
  • 1,774
  • 13
  • 18
5

In addition to @Matth, if you want to combine multiple statements you can write:

l = ['1', '32', '523', '336']

[ x for x in l if "2" not in x and "3" not in x]

# Returns: ['1']

fString Example

l = ['1', '32', '523', '336']

stringValA = "2"
stringValB = "3"

print(f"{[ x for x in l if stringValA not in x and stringValB not in x ]}")

# Returns: ['1']
JayRizzo
  • 3,234
  • 3
  • 33
  • 49
J.Doe
  • 82
  • 1
  • 10
1

Problem you could have is that you are trying to modify the sequence l same time as you loop over it in for t loop.

Tony Veijalainen
  • 5,447
  • 23
  • 31