Is a always ordered (as it is in the example)?<div><br></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">&gt;&gt;&gt; from bisect import bisect_left</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">&gt;&gt;&gt; a = [[&#39;cat&#39;,2],[&#39;cat&#39;,5],[&#39;cat&#39;,9],[&#39;dog&#39;,6]]</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">&gt;&gt;&gt; i = bisect_left(a, [&#39;cat&#39;])</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">&gt;&gt;&gt; if i &lt; len(a):</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">...     print a[i]</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">... </font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">[&#39;cat&#39;, 2]</font></div>
<div><br></div><div>Do you absolutely want a dictionary as a result?</div><div><br></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">&gt;&gt;&gt; d = {}</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">&gt;&gt;&gt; [d.setdefault(*pair) for pair in a]</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">[2, 2, 2, 6]</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">&gt;&gt;&gt; d</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">{&#39;dog&#39;: 6, &#39;cat&#39;: 2}</font></div>
<div><br></div><div>  - Jeremy</div><div><br><div class="gmail_quote">On Mon, Jun 21, 2010 at 7:22 PM, Vikram <span dir="ltr">&lt;<a href="mailto:kpguy@rediffmail.com">kpguy@rediffmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Suppose i have this list:<br>
<br>
&gt;&gt;&gt; a = [[&#39;cat&#39;,2],[&#39;cat&#39;,5],[&#39;cat&#39;,9],[&#39;dog&#39;,6]]<br>
&gt;&gt;&gt; a<br>
[[&#39;cat&#39;, 2], [&#39;cat&#39;, 5], [&#39;cat&#39;, 9], [&#39;dog&#39;, 6]]<br>
<br>
Now, there is a nice way to obtain the value 9.<br>
<br>
&gt;&gt;&gt; z = dict(a)<br>
&gt;&gt;&gt; z<br>
{&#39;dog&#39;: 6, &#39;cat&#39;: 9}<br>
<br>
Is there any elegant way to extract the value 2? (Value corresponding to the first occurence of &#39;cat&#39; in the 2-D list).<br>
<br>
Thanks,<br><font color="#888888">
Vikram<br><table border="0" width="644" height="57" cellspacing="0" cellpadding="0" style="font-family:Verdana;font-size:11px;line-height:15px"><tbody><tr><td><a href="http://sigads.rediff.com/RealMedia/ads/click_nx.ads/www.rediffmail.com/signatureline.htm@Middle?" target="_blank"><img></a></td>
</tr></tbody></table></font><br>_______________________________________________<br>
Baypiggies mailing list<br>
<a href="mailto:Baypiggies@python.org">Baypiggies@python.org</a><br>
To change your subscription options or unsubscribe:<br>
<a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br></blockquote></div><br></div>