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
<p>Eg:</p>

<pre><code>List=["tata1","tata2","Tata3","mdkjd","djhdj","Tata8","t9","t2"]
</code></pre>

<p>Output should be:<code>["tata1","tata2","Tata3","t2"]</code></p>

<p>I tried :</p>

<pre><code>content = [item for item in List if terminal.lower().startswith('t')]
</code></pre>

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

<p>If yes, how?</p>

<p>I tried writing but it gives error.</p>

<p>You can use <code>and</code> to test multiple conditions.</p>

<pre><code>&gt;&gt;&gt; [i for i in l if i and i[0] in 'tT' and all(j not in i for j in '789')]
['tata1', 'tata2', 'Tata3', 't2']
</code></pre>

<p>So from left to right this will test:</p>

<ul>
<li>non empty string</li>
<li>starts with <code>'t</code>' or <code>'T'</code></li>
<li>has any of the digits <code>'7'</code>, <code>'8'</code>, or <code>'9'</code></li>
</ul>

<p>These conditions will "short circuit" meaning upon the first test failing, there is no need to check the following conditions as <code>False and (anything)</code> is always <code>False</code>.</p>

python python-2.7 python-3.x for-loop filter