On Fri, Jul 31, 2020 at 7:59 PM Steven D'Aprano <st...@pearwood.info> wrote:

> On Fri, Jul 31, 2020 at 04:08:43PM -0700, Guido van Rossum wrote:
> [...]
> > I'm guessing that indexing by 0, if it were possible, would be a
> convenient
> > idiom to implement the "first item" operation that has been requested
> > numerous times (at least for dicts).
>
> Indeed, that seems to be the only use-case which seems to be even
> remotely common. `dict.popitem` would do it, of course, but it also
> mutates the dict.
>
> The other simple solution is `next(iter(mydict.items()))`.
>

That one always makes me uncomfortable, because the StopIteration it raises
when the dict is empty might be misinterpreted. Basically I never want to
call next() unless there's a try...except StopIteration: around it, and
that makes this a lot less simple.


> The bottom line here seems to me, as far as I can tell, is that being
> able to fetch a key and/or value by index is of only marginal use.
>

Agreed.


> > Slicing would be useful to get the
> > first N items of a huge dict without materializing the full list of items
> > as a list object, which brought Chris B to request this in the first
> place.
>
> The first request in this thread was from Hans:
>
>
> https://mail.python.org/archives/list/python-ideas@python.org/message/S7UMTWK65X6BJDYZ3SSU7I7HOIASDMMJ/
>
> He is using a dict to hold an array of columns indexed by name
> `{column_name: column}` and wanted to re-order and insert columns at
> arbitrary positions.
>

A bare dict is just not the data structure for that problem.

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ZAAYWB74TFXQTYXW2IWZNQNS235HOIJM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to