Why no list as dict key?

Chris Angelico rosuav at gmail.com
Wed Apr 20 16:43:00 EDT 2022


On Thu, 21 Apr 2022 at 06:20, Avi Gross via Python-list
<python-list at python.org> wrote:
>
> This does raise an issue, Chris, if you use the method of making a tuple companion for a list at a specific time just for use as a dictionary key, then later change the list, you can end up with various situations.
>

Yes. And those situations are *exactly* why you can't use a list as a key.

> Obviously the changed list can not only not access the stored item, but if converted again to a tuple, may address a different item. I can see many scenarios with abandoned dictionary items that are never deleted and can only be reached by examining all items in the dictionary.
> So mutability is only one concern. If you actually mutate your data, ...

And if you don't mutate your data, use a tuple.

ChrisA


More information about the Python-list mailing list