python loop to find the largest integer from a list
<p>I wrote a script to pull down a list of aws tags and then read the last octect and tell me which one is the highest IP. For example. here is the list of tags that are returned:</p>

<p>['vlslabmc, 172.16.0.13/24', 'vlslabmc,172.16.0.5/24', 'vlslabmc,172.16.0.3/24', 'vlslabmc,172.16.0.12/24', 'vlslabmc,172.16.0.16/24', 'vlslabmc,172.16.0.6/24', 'vlslabmc,172.16.0.1/24', 'vlslabmc,172.16.0.11/24', 'vlslabmc,172.16.0.15/24', 'vlslabmc,172.16.0.17/24', 'vlslabmc,172.16.0.4/24', 'vlslabmc,172.16.0.7/24', 'vlslabmc,172.16.0.10/24', 'vlslabmc,172.16.0.9/24', 'vlslabmc,172.16.0.8/24', 'vlslabmc,172.16.0.2/24', 'vlslabmc,172.16.0.14/24']</p>

<p>Here's my code to workout the largest IP from the tagLis (note that the largest is 17, 172.16.0.17)</p>

<pre><code> 21 def findLargestIP():
 22         for i in tagList:
 23                 #remove all the spacing in the tags
 24                 ec2Tags = i.strip()
 25                 #seperate any multiple tags
 26                 ec2SingleTag = ec2Tags.split(',')
 27                 #find the last octect of the ip address
 28                 fullIPTag = ec2SingleTag[1].split('.')
 29                 #remove the CIDR from ip to get the last octect
 30                 lastIPsTag = fullIPTag[3].split('/')
 31                 lastOctect = lastIPsTag[0]
 32                 ipList.append(lastOctect)
 33                 largestIP  = int(ipList[0])
 34                 for latestIP in ipList:
 35                         if int(latestIP) &gt; largestIP:
 36                                 largestIP = latestIP
 37         return largestIP
</code></pre>

<p>I'm not sure why.. but when I print the value of largestIP it always prints out 16. Ive gone through the code it should have worked <strong>(I'm avoiding using the max function as I'm just learning to code)</strong></p>

<p>Any help as aways is greatly appreciated.</p>

<p>Thanks</p>

<h2><strong>Edit with the answer below, and a question</strong></h2>

<p>Ok so thanks to a clue from cmarie I got it working the problem was mainly</p>

<pre><code>33                 largestIP  = int(ipList[0])
</code></pre>

<p>Here's the code running before with an added print statement on the list append:</p>

<pre><code>'13']
['13', '5']
['13', '5', '3']
['13', '5', '3', '12']
['13', '5', '3', '12', '16']
16
['13', '5', '3', '12', '16', '6']
16
['13', '5', '3', '12', '16', '6', '1']
16
['13', '5', '3', '12', '16', '6', '1', '11']
16
... ...
['13', '5', '3', '12', '16', '6', '1', '11', '15', '17', '4', '7', '10', '9', '8', '2']
    16
['13', '5', '3', '12', '16', '6', '1', '11', '15', '17', '4', '7', '10', '9', '8', '2', '14']
16
</code></pre>

<p>Basically what was happening is that during this loop :</p>

<pre><code>33                 largestIP  = int(ipList[0])
 34                 for latestIP in ipList:
 35                         if int(latestIP) &gt; largestIP:
 36                                 largestIP = latestIP
</code></pre>

<p>The loop stops at the 1st largest integer. in this case that is 16. <strong>*I'm not sure why it does but it does</strong></p>

<p>Here's the working code:</p>

<pre><code>19 def findLargestIP():
 20         ipList =[]
 21         for i in tagList:
 22                 #remove all the spacing in the tags
 23                 ec2Tags = i.strip()
 24                 #seperate any multiple tags
 25                 ec2SingleTag = ec2Tags.split(',')
 26                 #find the last octect of the ip address
 27                 fullIPTag = ec2SingleTag[1].split('.')
 28                 #remove the CIDR from ip to get the last octect
 29                 lastIPsTag = fullIPTag[3].split('/')
 30                 lastOctect = lastIPsTag[0]
 31                 ipList.append(int(lastOctect))
 32                 print ipList
 33                 largestIP  = 0
 34                 for latestIP in ipList:
 35                         if latestIP &gt; largestIP:
 36                                 largestIP = latestIP
 37                                 print latestIP
 38         print largestIP
 39         return largestIP
</code></pre>

<p>and the result:</p>

<pre><code>[13, 5, 3, 12, 16]
13
16
[13, 5, 3, 12, 16, 6]
13
16
[13, 5, 3, 12, 16, 6, 1]
13
16
[13, 5, 3, 12, 16, 6, 1, 11]
13
16
[13, 5, 3, 12, 16, 6, 1, 11, 15]
13
16
[13, 5, 3, 12, 16, 6, 1, 11, 15, 17]
13
16
17
</code></pre>

<p>Note it found 17.</p>

<p>Although other people have already provide you some alternative ways to find the answer, if you want to keep using your program, here is some way of fixing it:</p>

<pre><code>def findLargestIP():
    ipList = []
    for i in tagList:
        #remove all the spacing in the tags
        ec2Tags = i.strip()
        #seperate any multiple tags
        ec2SingleTag = ec2Tags.split(',')
        #find the last octect of the ip address
        fullIPTag = ec2SingleTag[1].split('.')
        #remove the CIDR from ip to get the last octect
        lastIPsTag = fullIPTag[3].split('/')
        lastOctect = lastIPsTag[0]
        ipList.append(int(lastOctect))
    largestIP  = 0
    for latestIP in ipList:
        if latestIP &gt; largestIP:
            largestIP = latestIP
    return largestIP
</code></pre>

<p>The differences from this and your program are that here I:</p>

<ul>
<li>set <code>ipList = []</code></li>
<li>make ipList contain integers, rather than strings</li>
<li>set <code>largestIP = 0</code>, instead of taking the first number of the ipList (since you shouldn't assume the list is sorted)</li>
<li>remove the loop to find the largest number outside the first loop [on the <code>tagList</code>] - just for eliminating unnecessary iterations</li>
</ul>

<p>If I would do that task, however, I would try to use regular expressions. Here is a way to do it:</p>

<pre><code>import re
def alternativeFindLargestIP():
    ipList = re.findall(r'(?&lt;=\.)\d+(?=/)', ' '.join(tagList))
    ipList = [int(num) for num in ipList]
    return max(ipList)
</code></pre>

python list loops integer