Nikita Gupta Nikita Gupta - 1 month ago 8
Python Question

One line python code to remove all the strings that doesn't start with T or t and if they contain number greater then 6

Eg:

List=["tata1","tata2","Tata3","mdkjd","djhdj","Tata8","t9","t2"]


Output should be:
["tata1","tata2","Tata3","t2"]


I tried :

content = [item for item in List if terminal.lower().startswith('t')]


My doubt if I can append one more condition with the if I used in my code?

If yes, how?

I tried writing but it gives error.

Answer

Matching your required output requires using all and not in to check that none of the '789' characters are contained in your input string while also checking for the starting characters:

res = [s for s in List if s[0] in ('tT') and all(j not in s for j in '789')]

Now res contains:

['tata1', 'tata2', 'Tata3', 't2']
Comments