On Tue, Feb 28, 2017 at 7:16 AM, Michel Desmoulin <[email protected]
> wrote:
> Le 28/02/2017 à 15:45, Steven D'Aprano a écrit :
> > No you don't. You can use slicing.
> > alist = [1, 2, 3]
> > print(alist[99:100]) # get the item at position 99
>
> No this gives you a list of one item or an empty list.
>
> dict.get('key', default_value) let you get a SCALAR value, OR a default
> value if it doesn't exist.
>
x = (alist[pos:pos+1] or [default_val])[0]
> How so ? "get the element x or a default value if it doesn't exist" seem
> at the contrary, a very robust approach.
>
Yes, and easily written as above. What significant advantage would it have
to spell the above as:
x = alist.get(pos, default_val)
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.
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/