How to do type verification in python?
<p>(Maybe b/c I'm from a C++ world)
I want to verify some python variable is</p>

<p><code>list(string)</code> or <code>list(dict(int, string))</code> or <code>SomethingIterable(string)</code></p>

<p>Is there a simple and unified way to do it?
(Instead of writing customized code to iterate and verify each instance..)</p>

<p>I emphasize that I understand in Python <code>list</code> can have elements of different types, which is exactly the reason why I ask how to verify a <code>list</code> which are composed by just a certain type e.g. <code>string</code>.</p>

<p>It seems that you are looking for an array (<a href="https://docs.python.org/3/library/array.html" rel="nofollow"><code>array.array</code></a>), not a list:</p>

<pre><code>&gt;&gt;&gt; l = [1]
&gt;&gt;&gt; l.append('a')
&gt;&gt;&gt; import array
&gt;&gt;&gt; a = array.array('l')
&gt;&gt;&gt; a.append(3)
&gt;&gt;&gt; a.append('a')
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: an integer is required (got type str)
&gt;&gt;&gt; a
array('l', [3])
</code></pre>

<p>As you get more and more comfortable with Python, though, you will gradually learn how to structure your code in such a way that type-checking becomes unnecessary. Personally, I've never had to use an <code>array.array</code> in Python (except in cases like this, where I'm specifically working with that module).</p>

python