[Python-ideas] get() method for list and tuples

Chris Barker chris.barker at noaa.gov
Fri Mar 3 16:21:30 EST 2017


On Fri, Mar 3, 2017 at 12:21 PM, Sven R. Kunze <srkunze at mail.de> wrote:

> For my part, I think casting a list to a dict is often the RIGHT way to
> address these issues.
>
>
> You can't be serious about this. Especially because it would negate your
> response to Ed "conditional on a len() call is the way to go". Now you tell
> people to use "convert to dict".
>

I am serious. It depends on the use case. If the data are an arbitrary-size
collection of essentially the same thing, then a sequence is the right data
structure, and examining len() (or catching the IndexError, maybe) is the
right way to handle there being fewer than you expect of them. I think the
key point is there there is nothing particularly different about them -- in
fact, often order isn't important at all.

If the data in question is a bunch of stuff where it matters where they
land in the sequence, and there may be missing values (Like a row in a CSV
file, maybe) then a dict IS the right structure -- even if it has integer
keys.

This reminds me of a discussion by Guido years ago about the "usual" use
cases for lists vs tuples -- lists are often a homogenous sequence of
items, whereas tuples are more likely to be heterogeneos -- more like a
struct

Now that I write that -- maybe the structure you really want is a
namedtuple. and maybe IT should have a get() so as to avoid catching
attribute errors all over teh place...

though getattr() does have a default -- so maybe namedtuple IS the answer
:-)

-CHB


-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170303/a28c429d/attachment.html>


More information about the Python-ideas mailing list