Order of operations in a dictionary comprehension

后端 未结 4 1814
醉话见心
醉话见心 2021-02-04 01:23

I came across the following interesting construct:

assuming you have a list of lists as follows:

my_list = [[\'captain1\', \'foo1\', \'bar1\', \'foobar1\         


        
4条回答
  •  别跟我提以往
    2021-02-04 01:55

    Actually your observation doesn't require special ordering of the operation. The reason is that x.pop(0) modifies the object x. So whether you evaluate the value (x) before or after the key (x.pop(0)) doesn't matter in this case.

    Anyway I don't think the python language specification prescribes a certain order of operations, which means that you should not rely on the order being any particular.

    Actually the standard implementation happens to evaluate the value before it evaluates the key, but there's nowhere in the standard where this is stated. The only guarantee is that the key-value pairs are evaluating in iteration order and they are inserted in that order.

提交回复
热议问题