[ACCEPTED]-Is the order of results coming from a list comprehension guaranteed?-list-comprehension

Accepted answer
Score: 42

Yes, the list comprehension preserves the 6 order of the original iterable (if there 5 is one). If the original iterable is ordered 4 (list, tuple, file, etc.), that's the order 3 you'll get in the result. If your iterable 2 is unordered (set, dict, etc.), there are 1 no guarantees about the order of the items.

Score: 3

Yes, a list is a sequence. Sequence order 1 is significant.

Score: 0

It has been a while, but since I came up 9 with a similar question myself recently, and 8 needed a bit more explanation to understand 7 what this comes down to exactly, I'll add 6 my two cents, may it help someone else in 5 the future! :) More specifically this is about the order of values resulting from a list comprehension operation.

Imagine you have the following 4 list:

list_of_c = [a, b, c, d, e]

I want to round the variables in that list 3 using the following list comprehension:

list_of_d = [round(value, 4) for value in list_of_c]

My question was whether this would mean that the order resulting from the list comprehension would be the following:

list_of_d = [round_a, round_b, round_c, round_d, round_e]

And 2 the answer I received very kindly from @juanpa.arrivillaga 1 , was that indeeded, YES that was the case!

More Related questions