How to check if a list contains a string
<p>I need to iterate through a list and check if the value is a string or an int. Is there any easy way to do this in python?</p>

<p>For example:</p>

<p><code>[1,2,3]</code> would be true.</p>

<p><code>["a",2,3]</code> would be false.</p>

<p>You could do this using <a href="https://docs.python.org/3/library/functions.html#all" rel="nofollow">all</a>, which would short circuit once a false condition is met. </p>

<pre><code>&gt;&gt;&gt; my_list = [1, 2, 3]
&gt;&gt;&gt; all(type(d) == int for d in my_list)
True

&gt;&gt;&gt; my_list = ['1', 2, 3]
&gt;&gt;&gt; all(type(d) == int for d in my_list)
False
</code></pre>

<p><a href="https://docs.python.org/3/library/functions.html#isinstance" rel="nofollow">isinstance</a> could be used when calling <code>all</code> as well: </p>

<pre><code>&gt;&gt;&gt; my_list = [1, 2, 3]
&gt;&gt;&gt; all(isinstance(d, int) for d in my_list)
True

&gt;&gt;&gt; my_list = ['1', 2, 3]
&gt;&gt;&gt; all(isinstance(d, int) for d in my_list)
False
</code></pre>

python list