In a ordinary loop it would be like this.
Can also remove the list as it's make a
>>> string = 'The quick brown fox jumps over the lazy dog. ' >>> characters = 'aeiou' >>> >>> lst = [] >>> for c in string: ... if c not in characters: ... lst.append(c) ... >>> print(''.join(lst)) Th qck brwn fx jmps vr th lzy dg.So a list comprehension construct make code over in line.
Can also remove the list as it's make a
generator object
first.>>> g = (c for c in string if c not in characters) >>> g <generator object <genexpr> at 0x03C8C8B0> >>> next(g) 'T' >>> next(g) 'h'See that there is a small difference no
[]
,and it still work as ''.join()
run the generator expressions
.>>> print(''.join(c for c in string if c not in characters)) Th qck brwn fx jmps vr th lzy dg.