Check if item in a Python list is an int/number
<p>I have a Python script that reads in a .csv file and stores each of the values into a list of lists: list[x][y].  I don't have any issues with this. </p>

<pre><code>list = []
i = 0

for row in reader:
     list.append([])
     list[i].append(row[0])
     ...
     i += 1
</code></pre>

<p>I want to check one of these fields to see if it's an number (integer).</p>

<p>When I perform a <code>print type(list[i][0])</code> it returns a <code>&lt;type 'str'&gt;</code> even though the value is say 100.</p>

<p>The if statements below are in a <code>for</code> loop iterating through the lists so what I was thinking of doing is doing a check:</p>

<pre><code>if type(list[i][0] == types.IntType):
     True
else: 
     False
</code></pre>

<p>This works, however that's frowned upon in PEP8 and so I should be using <code>isinstance()</code>, therefore I have modified it to</p>

<pre><code># check if a value is entered
if list[i][0] != '':
    if isinstance(int(list[i][0]), int):
        True
    else: 
        False
else
    False 
</code></pre>

<p>But I run into the problem of trying to convert a string to an int (if the user enters a string).</p>

<p>How do I overcome this?  It seems like a simple issue however I am new to Python so I was wondering of a neat and efficient way of dealing with this.  Should I be checking if the value is an int before storing it into the list?</p>

<p>I am using Python2. </p>

<p>Thanks</p>

<p>edit: I have wrapped the <code>isinstance()</code> check around a try exception catch however I feel like I shouldn't have to resort to this just to check if something is an int or not?  Just curious if there was a neater way to do this. </p>

<p>edit: I have used <code>isdigit</code> as mentioned previously however I was getting negative results. </p>

<p>i.e. given this data set.  list[0][0] = 123, list[1][0] = asdasd</p>

<pre><code>for i in range(0, 1):
   if (list[i][0]).isdigit:
       tempInt = list[i][0]
       print type(tempInt)
       print 'True: ' + tempInt
   else: 
       tempInt = 1
       print 'False: ' + tempInt
</code></pre>

<p>Results:</p>

<pre><code>&lt;type 'str'&gt;
True: 123
&lt;type 'str'&gt;
True: asdasd
</code></pre>