<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Second largest value from list in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/second-largest-value-from-list/m-p/489618#M38288</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migrated-users/3420" target="_blank"&gt;Joshua Bixby&lt;/A&gt;​&amp;nbsp; It is the statistician in me...one never removes duplicates from a list since all observations are equal...I was simply accessing via slicing, the second largest in a list without changing the length of the list...which if you did...would mean that you would be working with a different list and not the one in question. And your question would have to be re-posed&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; a = [5,5,5,5,5,5]
&amp;gt;&amp;gt;&amp;gt; a = [5,5,5,5,5]
&amp;gt;&amp;gt;&amp;gt; a[-2]
5
&amp;gt;&amp;gt;&amp;gt; N = len(a)
&amp;gt;&amp;gt;&amp;gt; b = list(set(a))
&amp;gt;&amp;gt;&amp;gt; b[-2]
Traceback (most recent call last):
&amp;nbsp; File "&amp;lt;interactive input&amp;gt;", line 1, in &amp;lt;module&amp;gt;
IndexError: list index out of range
&amp;gt;&amp;gt;&amp;gt; a[-2]
5
&amp;gt;&amp;gt;&amp;gt; b == a
False
&amp;gt;&amp;gt;&amp;gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 21:32:53 GMT</pubDate>
    <dc:creator>DanPatterson_Retired</dc:creator>
    <dc:date>2021-12-11T21:32:53Z</dc:date>
    <item>
      <title>Second largest value from list</title>
      <link>https://community.esri.com/t5/python-questions/second-largest-value-from-list/m-p/489614#M38284</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi folks,&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know how to use the inbuilt python function to derive the maximum value across a number of input fields.&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I am now trying to do is to derive the &lt;STRONG&gt;second largest&lt;/STRONG&gt; value across a number of input fields, and to retain the name of the that field.&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example if I have a set of fields as below I'd like to return "Area 3" and 15 from this list...&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Area 1 = 1&lt;/P&gt;&lt;P&gt;Area 2&amp;nbsp; = 5 &lt;/P&gt;&lt;P&gt;Area 3 = 15&lt;/P&gt;&lt;P&gt;Area 4 = 16 &lt;/P&gt;&lt;P&gt;Area 5 = 10&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas? Is there some sort of combination of rank or sort I can use that will give me the answer?&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Dave&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Feb 2015 15:14:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/second-largest-value-from-list/m-p/489614#M38284</guid>
      <dc:creator>DaveMiller</dc:creator>
      <dc:date>2015-02-17T15:14:06Z</dc:date>
    </item>
    <item>
      <title>Re: Second largest value from list</title>
      <link>https://community.esri.com/t5/python-questions/second-largest-value-from-list/m-p/489615#M38285</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you know python and can adapt this, follow the example&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; a = [3,5,2,4,1]&amp;nbsp;&amp;nbsp; # take your list
&amp;gt;&amp;gt;&amp;gt; a.sort()&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # sort it
&amp;gt;&amp;gt;&amp;gt; a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # have a look-see
[1, 2, 3, 4, 5]
&amp;gt;&amp;gt;&amp;gt; a[-1]&amp;nbsp; # max&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # get the max by indexing from the end
5
&amp;gt;&amp;gt;&amp;gt; a[-2]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # 2nd largest...same old idea
4
&amp;gt;&amp;gt;&amp;gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 21:32:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/second-largest-value-from-list/m-p/489615#M38285</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T21:32:47Z</dc:date>
    </item>
    <item>
      <title>Re: Second largest value from list</title>
      <link>https://community.esri.com/t5/python-questions/second-largest-value-from-list/m-p/489616#M38286</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Dan - will give this a whirl.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It looks straightforward enough!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Dave&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Feb 2015 15:46:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/second-largest-value-from-list/m-p/489616#M38286</guid>
      <dc:creator>DaveMiller</dc:creator>
      <dc:date>2015-02-17T15:46:58Z</dc:date>
    </item>
    <item>
      <title>Re: Second largest value from list</title>
      <link>https://community.esri.com/t5/python-questions/second-largest-value-from-list/m-p/489617#M38287</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Regarding &lt;A href="https://community.esri.com/migrated-users/3116" target="_blank"&gt;Dan Patterson&lt;/A&gt;​'s suggestion, it is likely the most straightforward or simplest since it relies on using built-in list methods and slicing, but there are still some things you should think about.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;One, can the second-maximum item be the same as the maximum?&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; a = [5, 3, 5, 4, 1, 2]
&amp;gt;&amp;gt;&amp;gt; a.sort()
&amp;gt;&amp;gt;&amp;gt; a[-1]
5
&amp;gt;&amp;gt;&amp;gt; a[-2]
5&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If not, collapsing the list into a set is one way to address the issue.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; a = [5, 3, 5, 4, 1, 2]
&amp;gt;&amp;gt;&amp;gt; a = list(set(a))
&amp;gt;&amp;gt;&amp;gt; a.sort()
&amp;gt;&amp;gt;&amp;gt; a[-1]
5
&amp;gt;&amp;gt;&amp;gt; a[-2]
4&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Second, sorting lists in Python is generally O(n log n) while getting max or min is O(n) (&lt;A href="https://wiki.python.org/moin/TimeComplexity" rel="nofollow noopener noreferrer" target="_blank"&gt;TimeComplexity - Python Wiki&lt;/A&gt;) .&amp;nbsp; If you are working with large lists, especially extremely large ones, the overhead of sorting the list to find the second highest maximum won't be trivial.&amp;nbsp; If you are working with large lists and performance matters, there is a good discussion thread over at stackoverflow on finding the second largest value:&amp;nbsp; &lt;A href="http://stackoverflow.com/questions/16225677/get-the-second-largest-number-in-a-list-in-linear-time" rel="nofollow noopener noreferrer" target="_blank"&gt;Get the second largest number in a list in linear time&lt;/A&gt;.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 21:32:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/second-largest-value-from-list/m-p/489617#M38287</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-11T21:32:50Z</dc:date>
    </item>
    <item>
      <title>Re: Second largest value from list</title>
      <link>https://community.esri.com/t5/python-questions/second-largest-value-from-list/m-p/489618#M38288</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migrated-users/3420" target="_blank"&gt;Joshua Bixby&lt;/A&gt;​&amp;nbsp; It is the statistician in me...one never removes duplicates from a list since all observations are equal...I was simply accessing via slicing, the second largest in a list without changing the length of the list...which if you did...would mean that you would be working with a different list and not the one in question. And your question would have to be re-posed&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; a = [5,5,5,5,5,5]
&amp;gt;&amp;gt;&amp;gt; a = [5,5,5,5,5]
&amp;gt;&amp;gt;&amp;gt; a[-2]
5
&amp;gt;&amp;gt;&amp;gt; N = len(a)
&amp;gt;&amp;gt;&amp;gt; b = list(set(a))
&amp;gt;&amp;gt;&amp;gt; b[-2]
Traceback (most recent call last):
&amp;nbsp; File "&amp;lt;interactive input&amp;gt;", line 1, in &amp;lt;module&amp;gt;
IndexError: list index out of range
&amp;gt;&amp;gt;&amp;gt; a[-2]
5
&amp;gt;&amp;gt;&amp;gt; b == a
False
&amp;gt;&amp;gt;&amp;gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 21:32:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/second-largest-value-from-list/m-p/489618#M38288</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T21:32:53Z</dc:date>
    </item>
    <item>
      <title>Re: Second largest value from list</title>
      <link>https://community.esri.com/t5/python-questions/second-largest-value-from-list/m-p/489619#M38289</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I can see the need to deal with duplicate "second highest" values being found.&amp;nbsp; If you need maintain those (ie. keep the duplicates) then the pandas Data Frame might be of use.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just for an example I created a .csv from the OP's data source sample and added an extra row that is a duplicate of Area 3 (the 2nd highest value):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Area 1 = 1&lt;/P&gt;&lt;P&gt;Area 2&amp;nbsp; = 5 &lt;/P&gt;&lt;P&gt;Area 3 = 15&lt;/P&gt;&lt;P&gt;Area 4 = 16 &lt;/P&gt;&lt;P&gt;Area 5 = 10&lt;/P&gt;&lt;P&gt;Area 6 = 15&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edit: this is far more simple than I had originally posted.&amp;nbsp; Just overlooked some of the powerful capability of this library.&amp;nbsp; Just simply determine the second highest value then return the rows equal to that value:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;data = r'H:\RankData.csv'
df = pd.io.parsers.read_table(data, sep=',')
secondval = df['Values'].max()
new_frame = df[df['Values'] == secondval-1]
print new_frame&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 21:32:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/second-largest-value-from-list/m-p/489619#M38289</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-11T21:32:56Z</dc:date>
    </item>
  </channel>
</rss>

