Nikita Gupta Nikita Gupta - 1 year ago 50
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



Output should be:

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 Source

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']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download