0

I have a dict that looks like this:

{1: [(7.8121826300806522e-06, 0.0027950172807843614),
(4.8435532306500046e-05, 0.006959395541691121),
(4.6873095780483916e-06, 0.002165014458884123),
(2.9686293994306483e-05, 0.005728026341619567),
...

and so on. However, when I pass this dict to a function where I do the following:

for key, vals in dic.items():
    for i in range(len(vals)):
        mean, std = vals[i]
        print(mean, std)

the values some out in a completely different order:

0.194110864246 0.48917376306916205
0.00532478368066 0.07417999717478747
0.00590132275876 0.07746526118007628
0.192434369854 0.447194389625775
0.0230381265761 0.17699968340608854
0.00160774718527 0.048076851436196597
0.0394265232975 0.19895854591008688
5.93725879886e-05 0.00790531061606315

However, if I take that block of code out of the function and run it on the dic before passing it into a function it works as expected, printing in the same order that shown at the top of the post.

My questions are essentially.... why? how do I fix this behavior? Is there a better way to do this?

Edit: This is not a duplicate of Python dictionary, how to keep keys/values in same order as declared?

^ the Order of my keys do not matter. The "completely different order" of values I have in the post are the values that print, in order, for the list in key 1. Which is why it is weird, as it is a different order.

For example:

(7.8121826300806522e-06, 0.0027950172807843614)

Might end up printing out as the 11th item or so, rather than the first

Community
  • 1
  • 1
daredevil1234
  • 1,303
  • 1
  • 10
  • 34
  • 3
    The notion of order does not apply to a dictionary. If you want order consider using `collections.OrderedDict` – Moses Koledoye Nov 01 '16 at 16:05
  • Sorry, I think there has been some confusion. What order the keys come in is not the problem. it is the order of the list itself that is coming out strangely. The values of "completely different order" i posted above, are the values of the list associated to key 1 as they print out with the code – daredevil1234 Nov 01 '16 at 16:13
  • The order in which `vals` (your lists) come from `.items()` is arbitrary. The items in each list will **always** come in the same order, unless of course you have a set. – Moses Koledoye Nov 01 '16 at 16:17
  • items() returns (key, vals) where vals is the list associated to the specific key. the order of Items() is not what is important here, as the list associated to the key will always be the same – daredevil1234 Nov 01 '16 at 16:22
  • And be sure there isn't another list in that dict that has the same values as the first, with a different order. Otherwise, no one can reproduce this problem. Lists are ordered. – Moses Koledoye Nov 01 '16 at 16:22
  • @MosesKoledoye items() returns a iterator for the 2-tuples of (key, vals) pairs. The value of the vals will **NEVER** be disassociated with key, while the 2-tuples come out in an order the is efficient for iteration. My vals is a list of (mean, std) tuples, not a set or anything else. – daredevil1234 Nov 01 '16 at 16:29
  • Well then, provide a [MCVE] with a sample of the dataset you're using. I still think you're overlooking something. That lists are ordered is not up for debate. – Moses Koledoye Nov 01 '16 at 16:33

0 Answers0