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 can use a combination of <a href="https://docs.python.org/3/library/functions.html#any" rel="nofollow"><code>any()</code></a> and <a href="https://docs.python.org/3/library/functions.html#isinstance" rel="nofollow"><code>isinstance()</code></a>:</p>

<pre><code>In [1]: def contains_string(l):
    ...:     return any(isinstance(item, basestring) for item in l)
    ...: 

In [2]: contains_string([1,2,3])
Out[2]: False

In [3]: contains_string(['a',2,3])
Out[3]: True
</code></pre>

<p><code>basestring</code> handles both "unicode" and "str" string types:</p>

<ul>
<li><a href="http://stackoverflow.com/questions/1979004/what-is-the-difference-between-isinstanceaaa-basestring-and-isinstanceaaa">What is the difference between isinstance(&#39;aaa&#39;, basestring) and isinstance(&#39;aaa&#39;, str)?</a></li>
</ul>

<p>Note that <code>any()</code> would <em>short-circuit</em> as well once it knows the result, see more about that here:</p>

<ul>
<li><a href="http://stackoverflow.com/questions/19389490/how-do-pythons-any-and-all-functions-work">How do Python&#39;s any and all functions work?</a></li>
</ul>

python list