Perhaps a NamedList would be helpful here?

One could write one fairly easily.

-CHB


On Fri, Jun 26, 2020 at 6:13 PM Steven D'Aprano <steve@pearwood.info> wrote:
On Fri, Jun 26, 2020 at 10:45:07AM -0700, Brett Cannon wrote:

> Why can't you do `tuple(dict.items())` to get your indexable pairs?

I don't think that an immutable copy is going to help Hans with his
use-case, since he already mentions that tuples don't solve his problem.

Swapping to a list gives you a mutable sequence that makes inserting
columns easy, but now lookups by column name are O(N) instead of O(1).

Hans, I think that a dict is probably not the best solution here, but
you can use a dict in an augmented data structure. I would consider
keeping your columns in a list, indexed by position, and keeping a table
of columns to index in a dict. Whenever you insert or remove a column,
you can update the table.

If this sounds like a lot of work, yes, it probably is, and making dicts
perform that work for *every single dict* would slow down the language a
lot. Dicts are critical for speed and performance because they are used
so extensively. Globals, builtins, almost every module, class and
instance use dicts internally, so they need to be as fast as possible.

If your experience with Perl tells you differently, please explain to us
how Perl hashes work in order that they support both fast hash indexing
and positional indexing at the same time.


--
Steven
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@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/POHINM764UHZKVXK3GWOYIJPF525IDCS/
Code of Conduct: http://python.org/psf/codeofconduct/


--
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython