[Python] Python: will for loop over dict preserve the order in specific case

Discussão em 'Python' iniciado por Stack, Setembro 10, 2024.

  1. Stack

    Stack Membro Participativo

    I have created a dict with predifined keys, where the keys are strings and the values are arrays. Example:

    dict = {
    "p1": [1,2,3],
    "r1": [1,2,3],
    "o1": [1,2,3],
    "y1": [1,2,3],
    "w1": [1,2,3]
    }


    Then I have logic to manipulate the values of the value arrays(increase/decrease some of the numbers). At the end I'm using for cycle to iterate over the dict like that:

    [dict[f][1] for f in dict]


    In this case I'm not changing the structure and I'm not adding any new elements in the dict. The question is: Can I be sure that the loop will iterate over the elements in the same order as they were defined initially or I have to specify order to be sure?

    Continue reading...

Compartilhe esta Página