0

join in python joins the list elements and keys in dictionaries right?.

Whenever I use join to join a list the output is in the same order as the list. I know it not ordered and the output clearly differed when i gave the same in a file and in the interpreter.

My question is how interpreter remembers that and gives the same output everytime for that order. Is it in some sort of cache or??

>>> x = ['a','b','c']
>>> ','.join(x)
'a,b,c'
>>> x = ['c','b','a']
>>> ','.join(x)
'c,b,a'

using a dict

>>> z = {'a':3,'b':1,'c':4,'d':2}
>>> ','.join(z)
'b,d,a,c'
>>> z = {'a':3,'d':1,'c':4,'b':2}
>>> ','.join(z)
'c,d,a,b' 

So I concluded that it varies each time but when I give the same dictionary with a specific order after several instructions it still displays the output in some order which is the same everytime in the interpreter!

>>> z = {'foo':3,'bar':1,'egg':4,'spam':2}
>>> ','.join(z)
'egg,bar,foo,spam'
>>> z = {'bar':3,'foo':1,'egg':4,'spam':2}
>>> ','.join(z)
'egg,bar,foo,spam'
>>> z = {'bar':3,'foo':1,'spam':4,'egg':2}
>>> ','.join(z)
'spam,bar,foo,egg'
>>> z = {'foo':3,'bar':1,'egg':4,'spam':2}
>>> ','.join(z)
'egg,bar,foo,spam'

I maybe missing something out but how does the interpreter remember it. Clear explanations would help greatly.

NOTE: As seen in the comments mentioned by chris_rands I quote 'the dict iteration is fixed within an interpreter session because the environmental variable PYTHONHASHSEED is fixed'

is more along the lines of the answer I am looking for. Explanations would be great!.

void
  • 2,571
  • 2
  • 20
  • 35
  • 2
    Dictionaries have no sense of such ordering. – Moses Koledoye Jun 07 '17 at 10:38
  • 2
    Python Dictionary will never maintain Order. If you want the order of Dictionary to be maintained, use `OrderedDict` from `Collections` – voidpro Jun 07 '17 at 10:39
  • @voidpro in 3.6 - they do keep a declared order - before that - no. – Jon Clements Jun 07 '17 at 10:40
  • No my question is how does python remember that.. say I give _x={'a':1,'b':2,'c':3}'_ and join it in the first and second line of interpreter and after that i change x to _{'c':1,'b':2,'c':3}_ and then again after some 20 lines doing other code i change x to the first order.(My order which i keep track of) **Python clearly gives the same ouput as before** . It is not entirely random then right? – void Jun 07 '17 at 10:42
  • @JonClements You don't seem to understand my question. It is not about ordering. It is about how internally the same output is given everytime for a given dict no matter how many times i give it. I gave the same in a file and output differed!. But how does the interpreter remember that? – void Jun 07 '17 at 10:47
  • @JonClements yes Jon. Got to know. – voidpro Jun 07 '17 at 10:51
  • @s_vishnu it also depends on the Python version... the dupe still holds the information you need though. – Jon Clements Jun 07 '17 at 10:51
  • Thanks should dive in more – void Jun 07 '17 at 10:52
  • @s_vishnu in short - dictionaries (prior to 3.6) aren't "ordered" - however, in the 2.x series, they were "arbitrarily" ordered - the same `dict` would always give the same output across runs. In (might be 3.4/3.5 - I can't remember) - the `dict` was essentially "salted", so while the "ordering" would remain the same in the same run, it may/may not differ in another run. – Jon Clements Jun 07 '17 at 10:54
  • If you *must* guarantee insertion order than use a `collections.OrderedDict` - although that has extra overhead in keeping track of keys. – Jon Clements Jun 07 '17 at 10:58
  • This might be a good reading for you: http://www.laurentluce.com/posts/python-dictionary-implementation/ – Błotosmętek Jun 07 '17 at 10:59
  • 1
    @JonClements I do agree that this is not a dupe (or at least not that dupe). I think possibly the reason that the `dict` iteration is fixed within an interpreter session is because the environmental variable `PYTHONHASHSEED` is fixed – Chris_Rands Jun 07 '17 at 11:10
  • @Chris_Rands okay - I've reopened it - if you feel you can find a more suitable duplicate (I'm sure there is one as you mention) - feel free. I'm sure there's going to be a pile on of answering the question yet again though - so - be hasty if possible :p – Jon Clements Jun 07 '17 at 11:11
  • Can you explain more about `PYTHONHASHSEED` ? @Chris_Rands – void Jun 07 '17 at 11:21
  • 1
    @s_vishnu I am not sure if this is the correct explanation, will look into it later if I have time – Chris_Rands Jun 07 '17 at 11:31
  • Your dict output sample is **impossible to recreate**, at least not without restarting the interpreter in between (at which point a different random hash seed is created). – Martijn Pieters Jun 07 '17 at 12:10

2 Answers2

0

dictionaries dont have an order in python. You might randomly get the same order, but it's not guaranteed. If you need a dictionary with a fixed order, have a look at OrderedDict: https://docs.python.org/2/library/collections.html#collections.OrderedDict

olisch
  • 960
  • 6
  • 11
0

You can use sortedcontainers, and then join

from sortedcontainers import SortedDict
z={'egg': 4, 'foo': 3, 'bar': 1, 'spam': 2}
','.join(SortedDict(z))
#'bar,egg,foo,spam'

in natural case :

','.join(dict(SortedDict(z)))   #or   ','.join(z)
#'egg,foo,bar,spam'
khelili miliana
  • 3,730
  • 2
  • 15
  • 28