Does the for/in loop construct preserve order?

前端 未结 4 1036
谎友^
谎友^ 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

    A for loop's iteration order is controlled by whatever object it's iterating over. Iterating over an ordered collection like a list is guaranteed to iterate over elements in the list's order, but iterating over an unordered collection like a set makes almost no order guarantees.

提交回复
热议问题