Pythonic way to find integer in list of strings
<p>I have an array as follows:</p>

<pre><code>hour = ['01','02','12']
</code></pre>

<p>and I want </p>

<pre><code>h = 1
str(h) in hour
</code></pre>

<p>to return <code>True</code>. What would be the most "Pythonic" way to do this? I could of course pad <code>h</code> with a zero, but is there a better way?</p>

<pre><code>if [i for i in hour if h == int(i)]:
    print("found it")
</code></pre>

<p><code>[i for i in hour if h == int(i)]</code> is a list comprehension which basically means generating a list from an iterable object. It looks into <code>hours</code> which is a list which is iterable, and one at a time checks if the value is the same as <code>h</code>. We cast it to <code>int(i)</code> because we are comparing ints and <code>i</code> is a string. </p>

<p>In python an empty list <code>[]</code> returns false when using in if statements. So by combining a list comprehension, it allows us to quickly check if there is a value we care about or not. If  there's a list, that means the list comprehension found a value we care about. </p>

python arrays