re-awakening this thread, because I just happened on an actual real-world use case:
I need the first 255 items in a dict, as a dict. Order is important, and I can count on an order-preserving dict.
I ended up with:
from itertools import islice
smaller_dict = dict(islice(large_dict.items(), 0, 255))
which works, and isn't doing an unnecessary copying but it's pretty darn ugly, as far as I'm concerned.
I could also do it the old fashioned way:
smaller_dict = {}
for k, v in dict.items():
if i > 255:
break
smaller_dict[k] = v
which is worse.
I'd much rather:
dict(large_dict.items[:255])
I'd even more prefer:
dict[:255]
which would, in theory be possible, as slices are not hashable, so no one is doing this in current code as an actual dict key, but yeah, that's too much magic to ask for.
To be fair, this isn't that much data, and it's in memory already, so:
dict(tuple(large_dict.items())[:255])
would work just fine. but as Python has moved toward a lot more iterables, and lazy evaluation, it seems like we shouldn't have to make a full copy, just to pull a fraction of something out.
Note that this is related to an earlier thread I started about having a way to lazy-slice a Sequence -- essentially islice, but with slice syntax.
This is also related a bit to another thread about keywords in indexing -- there were examples there where it would be nice to have slice syntax in more places, like function calls. then this could be:
So: anyone have a cleaner way to accomplish this without any changes to Python?
This is still an oddball use case, so may not be a reason to change Python -- but it IS a real world, operational code, use case :-)
-CHB