Does the for/in loop construct preserve order?

前端 未结 4 1158
谎友^
谎友^ 2021-02-12 11:11

Does an ordinary for/in statement guarantee the list is iterated in order?

my_list = [5,4,3,2]
for i in my_list
    print(i)

That is, is the lo

4条回答
  •  情书的邮戳
    2021-02-12 11:22

    If you want to test it yourself:

    my_list = [5,4,3,2]
    
    for _ in range(20):
        new_list = []
        for i in my_list:
            new_list.append(i)
        print(my_list == new_list) 
    

    And just for a control case:

    print([2,4,5,3] == my_list)
    

提交回复
热议问题