Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Python dictionaries literals evaluated in order? [duplicate]

When I run this

import itertools

bar = itertools.count(1).next

print {'a': bar(), 'b': bar(), 'c': bar()}

on CPython, I get {'a': 0, 'b': 1, 'c': 2}. Is this consistent across implementations and/or documented anywhere? Will a, b and c ever be assigned values out of order?


1 Answers

The answer to your question can be found here (I do not have enough reputation to add this as a comment).

In sum, "Dictionary evaluation order should be the same as written, but there is a outstanding bug where values are evaluated before the keys.'

like image 139
unicorn_poet Avatar answered Feb 02 '26 00:02

unicorn_poet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!