Oct-23-2019, 08:54 AM
Main problem with your code is that you mutate the list while iterating over it (it can be done but you must know and understand what are you doing):
If your objective is: 'give me list of all chars in phrase which are in str1' then one can skip all conversions etc and have list comprehension:
Quote:"If fact, in any programming language for most part if you mutate something while you iterating over it you living in state of sin and you deserve whatever happens to you" -- Raymond Hettinger, Python core-developer, Transforming Code into Beautiful, Idiomatic Python
If your objective is: 'give me list of all chars in phrase which are in str1' then one can skip all conversions etc and have list comprehension:
>>> phrase="don't panic" >>> str1="on tap" >>> [char for char in phrase if char in str1] ['o', 'n', 't', ' ', 'p', 'a', 'n']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.