returning a namedtuple on dict.items()
Hi, That is my first post here, I hope it's the right place for it. so I was using some dicts had the following code: env = dict(t for t in os.environ.items() if 'SUBSTRING' not in t[0]) and I thought: It could have really been nice if i could do: env = dict(t for t in os.environ.items() if 'SUBSTRING' not in t.key) now I see that environ is not exactly a dict, but i think this is useful for all dictionary types. on the other hand, dicts are everywhere and i'm not sure about the performance impact of this for those that just ignore the access by attribute. of course, a matching t.value must be in place as well. ;) I would have opened a PR just to try out, but I feel this is so in the core that I'd first like to hear some comments. I was searching if anyone else had this idea before but couldn't find anything. Thanks, Nadav
Nadav Wexler wrote:
Hi, That is my first post here, I hope it's the right place for it. so I was using some dicts had the following code: env = dict(t for t in os.environ.items() if 'SUBSTRING' not in t[0]) and I thought: It could have really been nice if i could do: env = dict(t for t in os.environ.items() if 'SUBSTRING' not in t.key) now I see that environ is not exactly a dict, but i think this is useful for all dictionary types. on the other hand, dicts are everywhere and i'm not sure about the performance impact of this for those that just ignore the access by attribute. of course, a matching t.value must be in place as well. ;) I would have opened a PR just to try out, but I feel this is so in the core that I'd first like to hear some comments. I was searching if anyone else had this idea before but couldn't find anything. Thanks, Nadav
`env = {key: value for key, value in os.environ.items() if 'SUBSTRING' not in key}`
31.03.20 20:15, Nadav Wexler пише:
Hi, That is my first post here, I hope it's the right place for it.
so I was using some dicts had the following code: env = dict(t for t in os.environ.items() if 'SUBSTRING' not in t[0])
and I thought: It could have really been nice if i could do: env = dict(t for t in os.environ.items() if 'SUBSTRING' not in t.key)
now I see that environ is not exactly a dict, but i think this is useful for all dictionary types. on the other hand, dicts are everywhere and i'm not sure about the performance impact of this for those that just ignore the access by attribute.
of course, a matching t.value must be in place as well. ;) I would have opened a PR just to try out, but I feel this is so in the core that I'd first like to hear some comments. I was searching if anyone else had this idea before but couldn't find anything.
This idea was proposed multiple times and was rejected. Python is optimized for raw tuples, it is one of most important type. Many operations with tuples are much faster than operations with named tuples (including creating and destroying). Tuples emitted by dict.item() (an enumerate(), and zip()) are usually short-living and produced in mass. So replacing tuples with named tuples would harm a lot of code which do not need attribute access.
that's what I assumed... thanks for the reply! :) On Tue, Mar 31, 2020 at 9:08 PM Serhiy Storchaka <storchaka@gmail.com> wrote:
31.03.20 20:15, Nadav Wexler пише:
Hi, That is my first post here, I hope it's the right place for it.
so I was using some dicts had the following code: env = dict(t for t in os.environ.items() if 'SUBSTRING' not in t[0])
and I thought: It could have really been nice if i could do: env = dict(t for t in os.environ.items() if 'SUBSTRING' not in t.key)
now I see that environ is not exactly a dict, but i think this is useful for all dictionary types. on the other hand, dicts are everywhere and i'm not sure about the performance impact of this for those that just ignore the access by attribute.
of course, a matching t.value must be in place as well. ;) I would have opened a PR just to try out, but I feel this is so in the core that I'd first like to hear some comments. I was searching if anyone else had this idea before but couldn't find anything.
This idea was proposed multiple times and was rejected. Python is optimized for raw tuples, it is one of most important type. Many operations with tuples are much faster than operations with named tuples (including creating and destroying). Tuples emitted by dict.item() (an enumerate(), and zip()) are usually short-living and produced in mass. So replacing tuples with named tuples would harm a lot of code which do not need attribute access. _______________________________________________ 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/VRVLQH... Code of Conduct: http://python.org/psf/codeofconduct/
On 31Mar2020 22:28, Nadav Wexler <blop.blopy@gmail.com> wrote:
that's what I assumed... thanks for the reply! :)
Also, there are modules to provide this kind of thing when you want it. For example, my cs.mappings module: https://pypi.org/project/cs.mappings/ has dicts_to_namedtuples() which converts an iterable of dicts to namedtuples, and the lower level function named_row_tuple(*keys) function used to create the specific namedtuple class from some keys (eg your_dict.keys()). There's also named_column_tuples() which presumes the column/attribute names come from the first data row. I wrote these when processing some data received as a spreadsheet, whose column names (from the human typed header row) tended to be stable, but the number and ordering of the columns were undependable. Feeding them through these (via an additional layer in cs.csvutils) got me nice tuples with readable attribute names. I'm sure there must be other modules with similar facilities as well. Cheers, Cameron Simpson <cs@cskk.id.au>
participants (4)
-
Cameron Simpson -
jdveiga@gmail.com -
Nadav Wexler -
Serhiy Storchaka