<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Feb 28, 2017 at 7:16 AM, Michel Desmoulin <span dir="ltr"><<a href="mailto:desmoulinmichel@gmail.com" target="_blank">desmoulinmichel@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">Le 28/02/2017 à 15:45, Steven D'Aprano a écrit :<br>> No you don't. You can use slicing.<br>> alist = [1, 2, 3]<br>
> print(alist[99:100]) # get the item at position 99<br>
<br>
</span>No this gives you a list of one item or an empty list.<br>
<br>
dict.get('key', default_value) let you get a SCALAR value, OR a default<br>
value if it doesn't exist.<br></blockquote><div><br></div><div> x = (alist[pos:pos+1] or [default_val])[0]</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">How so ? "get the element x or a default value if it doesn't exist" seem<br>
at the contrary, a very robust approach.<br></blockquote><div><br></div><div>Yes, and easily written as above. What significant advantage would it have to spell the above as:</div><div><br></div><div> x = alist.get(pos, default_val)</div><div><br></div><div>It's a couple characters shorter in the proposed version. I guess I'll concede that needing the odd indexing at the end to get the scalar is slightly ugly.</div></div><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons. Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br></div>
</div></div>