Bruno Bronosky
Bruno Bronosky

Reputation: 70339

Is the order of dictionary items reliable **at creation**?

Is the order of dictionary items reliable at creation?

NOTE: This is not a duplicate of all the questions where people expect a dict to behave like an OrderedDict.

What I want to do is pass a json dictionary to an AWS Lambda function. Much of the content of that dictionary will come from an open file handler. It goes something like this:

params = {
          "FunctionName": "log_load",
          "Payload": json.dumps({
                                 "header": fh.readline(),
                                 "start": fh.tell(),
                                 "data": fh.read(),
                                 "end": fh.tell()})}
result = lambda_client.invoke(**params)

It's important that all of those methods on the fh object be called in the order they are listed. I don't care about the order of the dictionary when it is read later.

Can I rely on those 4 functions being called in the order listed?

I have created a test below that gives me anecdotal evidence that I can. However, I can't even get a case where the pprint displays the dictionary out of order. So, I'm not surprised that the keys match the values.

from pprint import pprint
r = range(100)
i = r.__iter__()

d = {
     0: i.__next__(),
     1: i.__next__(),
     2: i.__next__(),
     3: i.__next__(),
     4: i.__next__(),
     5: i.__next__(),
     6: i.__next__(),
     7: i.__next__(),
     8: i.__next__(),
     9: i.__next__(),
     10: i.__next__(),
     11: i.__next__(),
     12: i.__next__(),
     13: i.__next__(),
     14: i.__next__(),
     15: i.__next__(),
     16: i.__next__(),
     17: i.__next__(),
     18: i.__next__(),
     19: i.__next__(),
     20: i.__next__(),
     21: i.__next__(),
     22: i.__next__(),
     23: i.__next__(),
     24: i.__next__(),
     25: i.__next__(),
     26: i.__next__(),
     27: i.__next__(),
     28: i.__next__(),
     29: i.__next__(),
     30: i.__next__(),
     31: i.__next__(),
     32: i.__next__(),
     33: i.__next__(),
     34: i.__next__(),
     35: i.__next__(),
     36: i.__next__(),
     37: i.__next__(),
     38: i.__next__(),
     39: i.__next__(),
     40: i.__next__(),
     41: i.__next__(),
     42: i.__next__(),
     43: i.__next__(),
     44: i.__next__(),
     45: i.__next__(),
     46: i.__next__(),
     47: i.__next__(),
     48: i.__next__(),
     49: i.__next__(),
     50: i.__next__(),
     51: i.__next__(),
     52: i.__next__(),
     53: i.__next__(),
     54: i.__next__(),
     55: i.__next__(),
     56: i.__next__(),
     57: i.__next__(),
     58: i.__next__(),
     59: i.__next__(),
     60: i.__next__(),
     61: i.__next__(),
     62: i.__next__(),
     63: i.__next__(),
     64: i.__next__(),
     65: i.__next__(),
     66: i.__next__(),
     67: i.__next__(),
     68: i.__next__(),
     69: i.__next__(),
     70: i.__next__(),
     71: i.__next__(),
     72: i.__next__(),
     73: i.__next__(),
     74: i.__next__(),
     75: i.__next__(),
     76: i.__next__(),
     77: i.__next__(),
     78: i.__next__(),
     79: i.__next__(),
     80: i.__next__(),
     81: i.__next__(),
     82: i.__next__(),
     83: i.__next__(),
     84: i.__next__(),
     85: i.__next__(),
     86: i.__next__(),
     87: i.__next__(),
     88: i.__next__(),
     89: i.__next__(),
     90: i.__next__(),
     91: i.__next__(),
     92: i.__next__(),
     93: i.__next__(),
     94: i.__next__(),
     95: i.__next__(),
     96: i.__next__(),
     97: i.__next__(),
     98: i.__next__(),
     99: i.__next__()}
pprint(d, width=-1, indent=4)

Upvotes: 3

Views: 66

Answers (1)

Francisco
Francisco

Reputation: 11476

Yes, Python evaluates expressions from left to right:

From the docs:

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

In the following lines, expressions will be evaluated in the arithmetic order of their suffixes:

expr1, expr2, expr3, expr4
(expr1, expr2, expr3, expr4)
{expr1: expr2, expr3: expr4}
expr1 + expr2 * (expr3 - expr4)
expr1(expr2, expr3, *expr4, **expr5)
expr3, expr4 = expr1, expr2

Upvotes: 5

Related Questions