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>So I had to refactor your code a little bit. I assumed ipList was an empty list. Are you sure you tested to see if it actually ran? Specifically your if statement</p>

<pre><code>if int(latestIP) &gt; largestIP:
    largestIP = latestIP
</code></pre>

<p>would return a</p>

<pre><code>TypeError: unorderable types: int() &gt; str()
</code></pre>

<p>because you would be assigning a string to largestIP and then in the next iteration, you would compare a string with an int. Aside from that, your code seems functional. It returns 17 as the largest last octet for me which seems right.</p>

<p>If your intention is to return the largest last octet of your list of ip addresses, you might want to approach this a little bit differently.</p>

<p><strong>Option 1:</strong> Accumulate list of IP Addresses first</p>

<p>Instead of nesting a for loop in your loop to iterate through all the tags, you can first just accumulate the tags and then go through and find the max. This way, you go through the tag list once, and then the ip list once, instead of going through your entire ip list each time you iterate through your tag list.</p>

<p><strong>Option 2:</strong> Create a list of only the last octet</p>

<p>Similar to option 1, you would iterate through your tagList and accumulate all the last octets of the ip addresses casted to ints into a list, instead of the entire ip address. In a loop afterwards, you can call max on the list with the octets (I'm guessing you want to avoid this).</p>

<p><strong>Option 3:</strong> Have a running greatest value</p>

<p>I think this is the best solution. As you're going through the tag list, you can keep a variable that will have the greatest last octet so far. This way you only need to loop through the tag list once, and still come out with the greatest last octet thus far.</p>

<p>If you want to grab the entire IP address, options 1 and 3 would still work, but for option 2, you might want to look into <a href="https://docs.python.org/3/tutorial/datastructures.html#dictionaries" rel="nofollow">python dictionaries</a>.</p>

python list loops integer