python dict not displayed in order [duplicate] - python

This question already has answers here:
How do I sort a dictionary by key?
(32 answers)
Closed 5 years ago.
Could you please tell me whats the difference between python dict on below two cases. First one prints the data in correct order but on second iteration P6 take precedence over P5. What will be the reason?
>>> a=["P3", "P4"]
>>> devices_by_dc = {}
>>> for b in a:
... devices_by_dc[b] = {}
>>> print devices_by_dc
{'P3': {}, 'P4': {}}
>>> a=["P5", "P6"]
>>> devices_by_dc = {}
>>> for b in a:
... devices_by_dc[b] = {}
{'P6': {}, 'P5': {}}

In Python, dictionaries are not ordered, they are a set-like objects. Therefore the order of printing it is completely random.
Consider dict.keys(). That is a set. So basically when you do
print(dict)
what actually happens is
for key in dict.keys():
str_out = ' \'{0}\': {1},'.format(key, dict[key]
print '{{ {0} }}'.format(str_out[:-1])
And since dict.keys() is a set, the order of selecting keys is random.
If you want to introduce order into dictionary, then instead of dict use collections.OrderedDict as suggested in the comment to the question. Don't forget to import collections.

Dictionaries in python are not ordered by default. You can use an OrderedDict instead.
import collections
devices_by_dc = collections.OrderedDict()
for b in a:
devices_by_dc[b] = {}

Related

Checking if JSON object exists [duplicate]

This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
I wanted to test if a key exists in a dictionary before updating the value for the key.
I wrote the following code:
if 'key1' in dict.keys():
print "blah"
else:
print "boo"
I think this is not the best way to accomplish this task. Is there a better way to test for a key in the dictionary?
in tests for the existence of a key in a dict:
d = {"key1": 10, "key2": 23}
if "key1" in d:
print("this will execute")
if "nonexistent key" in d:
print("this will not")
Use dict.get() to provide a default value when the key does not exist:
d = {}
for i in range(10):
d[i] = d.get(i, 0) + 1
To provide a default value for every key, either use dict.setdefault() on each assignment:
d = {}
for i in range(10):
d[i] = d.setdefault(i, 0) + 1
or use defaultdict from the collections module:
from collections import defaultdict
d = defaultdict(int)
for i in range(10):
d[i] += 1
Use key in my_dict directly instead of key in my_dict.keys():
if 'key1' in my_dict:
print("blah")
else:
print("boo")
That will be much faster as it uses the dictionary's O(1) hashing as opposed to doing an O(n) linear search on a list of keys.
You can test for the presence of a key in a dictionary, using the in keyword:
d = {'a': 1, 'b': 2}
'a' in d # <== evaluates to True
'c' in d # <== evaluates to False
A common use for checking the existence of a key in a dictionary before mutating it is to default-initialize the value (e.g. if your values are lists, for example, and you want to ensure that there is an empty list to which you can append when inserting the first value for a key). In cases such as those, you may find the collections.defaultdict() type to be of interest.
In older code, you may also find some uses of has_key(), a deprecated method for checking the existence of keys in dictionaries (just use key_name in dict_name, instead).
You can shorten your code to this:
if 'key1' in my_dict:
...
However, this is at best a cosmetic improvement. Why do you believe this is not the best way?
For additional information on speed execution of the accepted answer's proposed methods (10 million loops):
'key' in mydict elapsed time 1.07 seconds
mydict.get('key') elapsed time 1.84 seconds
mydefaultdict['key'] elapsed time 1.07 seconds
Therefore using in or defaultdict are recommended against get.
I would recommend using the setdefault method instead. It sounds like it will do everything you want.
>>> d = {'foo':'bar'}
>>> q = d.setdefault('foo','baz') #Do not override the existing key
>>> print q #The value takes what was originally in the dictionary
bar
>>> print d
{'foo': 'bar'}
>>> r = d.setdefault('baz',18) #baz was never in the dictionary
>>> print r #Now r has the value supplied above
18
>>> print d #The dictionary's been updated
{'foo': 'bar', 'baz': 18}
A dictionary in Python has a get('key', default) method. So you can just set a default value in case there isn't any key.
values = {...}
myValue = values.get('Key', None)
Using the Python ternary operator:
message = "blah" if 'key1' in my_dict else "booh"
print(message)
Use EAFP (easier to ask forgiveness than permission):
try:
blah = dict["mykey"]
# key exists in dict
except KeyError:
# key doesn't exist in dict
See other Stack Overflow posts:
Using 'try' vs. 'if' in Python
Checking for member existence in Python
Check if a given key already exists in a dictionary
To get the idea how to do that we first inspect what methods we can call on dictionary.
Here are the methods:
d={'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10}
Python Dictionary clear() Removes all Items
Python Dictionary copy() Returns Shallow Copy of a Dictionary
Python Dictionary fromkeys() Creates dictionary from given sequence
Python Dictionary get() Returns Value of The Key
Python Dictionary items() Returns view of dictionary (key, value) pair
Python Dictionary keys() Returns View Object of All Keys
Python Dictionary pop() Removes and returns element having given key
Python Dictionary popitem() Returns & Removes Element From Dictionary
Python Dictionary setdefault() Inserts Key With a Value if Key is not Present
Python Dictionary update() Updates the Dictionary
Python Dictionary values() Returns view of all values in dictionary
The brutal method to check if the key already exists may be the get() method:
d.get("key")
The other two interesting methods items() and keys() sounds like too much of work. So let's examine if get() is the right method for us. We have our dict d:
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10}
Printing shows the key we don't have will return None:
print(d.get('key')) #None
print(d.get('clear')) #0
print(d.get('copy')) #1
We use that to get the information if the key is present or no.
But consider this if we create a dict with a single key:None:
d= {'key':None}
print(d.get('key')) #None
print(d.get('key2')) #None
Leading that get() method is not reliable in case some values may be None.
This story should have a happier ending. If we use the in comparator:
print('key' in d) #True
print('key2' in d) #False
We get the correct results.
We may examine the Python byte code:
import dis
dis.dis("'key' in d")
# 1 0 LOAD_CONST 0 ('key')
# 2 LOAD_NAME 0 (d)
# 4 COMPARE_OP 6 (in)
# 6 RETURN_VALUE
dis.dis("d.get('key2')")
# 1 0 LOAD_NAME 0 (d)
# 2 LOAD_METHOD 1 (get)
# 4 LOAD_CONST 0 ('key2')
# 6 CALL_METHOD 1
# 8 RETURN_VALUE
This shows that in compare operator is not just more reliable, but even faster than get().
The ways in which you can get the results are:
if your_dict.has_key(key) Removed in Python 3
if key in your_dict
try/except block
Which is better is dependent on 3 things:
Does the dictionary 'normally has the key' or 'normally does not have the key'.
Do you intend to use conditions like if...else...elseif...else?
How big is dictionary?
Read More: http://paltman.com/try-except-performance-in-python-a-simple-test/
Use of try/block instead of 'in' or 'if':
try:
my_dict_of_items[key_i_want_to_check]
except KeyError:
# Do the operation you wanted to do for "key not present in dict".
else:
# Do the operation you wanted to do with "key present in dict."
Python 2 only: (and Python 2.7 supports `in` already)
You can use the has_key() method:
if dict.has_key('xyz')==1:
# Update the value for the key
else:
pass
Just an FYI adding to Chris. B's (best) answer:
d = defaultdict(int)
Works as well; the reason is that calling int() returns 0 which is what defaultdict does behind the scenes (when constructing a dictionary), hence the name "Factory Function" in the documentation.
A Python dictionary has the method called __contains__. This method will return True if the dictionary has the key, else it returns False.
>>> temp = {}
>>> help(temp.__contains__)
Help on built-in function __contains__:
__contains__(key, /) method of builtins.dict instance
True if D has a key k, else False.
Another way of checking if a key exists using Boolean operators:
d = {'a': 1, 'b':2}
keys = 'abcd'
for k in keys:
x = (k in d and 'blah') or 'boo'
print(x)
This returns
>>> blah
>>> blah
>>> boo
>>> boo
Explanation
First, you should know that in Python, 0, None, or objects with zero length evaluate to False. Everything else evaluates to True. Boolean operations are evaluated left to right and return the operand not True or False.
Let's see an example:
>>> 'Some string' or 1/0
'Some string'
>>>
Since 'Some string' evaluates to True, the rest of the or is not evaluated and there is no division by zero error raised.
But if we switch the order 1/0 is evaluated first and raises an exception:
>>> 1/0 or 'Some string'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>>
We can use this for pattern for checking if a key exists.
(k in d and 'blah')
does the same as
if k in d:
'blah'
else:
False
This already returns the correct result if the key exists, but we want it to print 'boo' when it doesn't. So, we take the result and or it with 'boo'
>>> False or 'boo'
'boo'
>>> 'blah' or 'boo'
'blah'
>>>
You can use a for loop to iterate over the dictionary and get the name of key you want to find in the dictionary. After that, check if it exist or not using if condition:
dic = {'first' : 12, 'second' : 123}
for each in dic:
if each == 'second':
print('the key exists and the corresponding value can be updated in the dictionary')

How to unpack dict with one key-value pair to two variables more elegantly? [duplicate]

This question already has answers here:
How to extract dictionary single key-value pair in variables
(11 answers)
Closed 3 years ago.
Currently, I'm using this:
d = {'a': 'xyz'}
k, v = list(*d.items())
The starred expression is required here, as omitting it causes the list function/constructor to return a list with a single tuple, which contains the key and value.
However, I was wondering if there were a better way to do it.
Keep nesting:
>>> d = {'a': 'xyz'}
>>> ((k,v),) = d.items()
>>> k
'a'
>>> v
'xyz'
Or equivalently:
>>> (k,v), = d.items()
>>> k
'a'
>>> v
'xyz'
>>>
Not sure which I prefer, the last one might be a bit difficult to read if I was glancing at it.
Note, the advantage here is that it is non-destructive and fails if the dict has more than one key-value pair.
Since dict items do not support indexed access, you might resort to the following non-mutating retrieval of the first (and only) item:
k, v = next(iter(d.items()))
This has the advantage of not only working for dicts of any size, but remaining an O(1) operation which other solutions that unpack the items or convert them to a list would not.
If you don't mind the dictionary getting altered, this'll do:
k, v = d.popitem()

Can't get function to add an item to a dictionnary [duplicate]

This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
I wanted to test if a key exists in a dictionary before updating the value for the key.
I wrote the following code:
if 'key1' in dict.keys():
print "blah"
else:
print "boo"
I think this is not the best way to accomplish this task. Is there a better way to test for a key in the dictionary?
in tests for the existence of a key in a dict:
d = {"key1": 10, "key2": 23}
if "key1" in d:
print("this will execute")
if "nonexistent key" in d:
print("this will not")
Use dict.get() to provide a default value when the key does not exist:
d = {}
for i in range(10):
d[i] = d.get(i, 0) + 1
To provide a default value for every key, either use dict.setdefault() on each assignment:
d = {}
for i in range(10):
d[i] = d.setdefault(i, 0) + 1
or use defaultdict from the collections module:
from collections import defaultdict
d = defaultdict(int)
for i in range(10):
d[i] += 1
Use key in my_dict directly instead of key in my_dict.keys():
if 'key1' in my_dict:
print("blah")
else:
print("boo")
That will be much faster as it uses the dictionary's O(1) hashing as opposed to doing an O(n) linear search on a list of keys.
You can test for the presence of a key in a dictionary, using the in keyword:
d = {'a': 1, 'b': 2}
'a' in d # <== evaluates to True
'c' in d # <== evaluates to False
A common use for checking the existence of a key in a dictionary before mutating it is to default-initialize the value (e.g. if your values are lists, for example, and you want to ensure that there is an empty list to which you can append when inserting the first value for a key). In cases such as those, you may find the collections.defaultdict() type to be of interest.
In older code, you may also find some uses of has_key(), a deprecated method for checking the existence of keys in dictionaries (just use key_name in dict_name, instead).
You can shorten your code to this:
if 'key1' in my_dict:
...
However, this is at best a cosmetic improvement. Why do you believe this is not the best way?
For additional information on speed execution of the accepted answer's proposed methods (10 million loops):
'key' in mydict elapsed time 1.07 seconds
mydict.get('key') elapsed time 1.84 seconds
mydefaultdict['key'] elapsed time 1.07 seconds
Therefore using in or defaultdict are recommended against get.
I would recommend using the setdefault method instead. It sounds like it will do everything you want.
>>> d = {'foo':'bar'}
>>> q = d.setdefault('foo','baz') #Do not override the existing key
>>> print q #The value takes what was originally in the dictionary
bar
>>> print d
{'foo': 'bar'}
>>> r = d.setdefault('baz',18) #baz was never in the dictionary
>>> print r #Now r has the value supplied above
18
>>> print d #The dictionary's been updated
{'foo': 'bar', 'baz': 18}
A dictionary in Python has a get('key', default) method. So you can just set a default value in case there isn't any key.
values = {...}
myValue = values.get('Key', None)
Using the Python ternary operator:
message = "blah" if 'key1' in my_dict else "booh"
print(message)
Use EAFP (easier to ask forgiveness than permission):
try:
blah = dict["mykey"]
# key exists in dict
except KeyError:
# key doesn't exist in dict
See other Stack Overflow posts:
Using 'try' vs. 'if' in Python
Checking for member existence in Python
Check if a given key already exists in a dictionary
To get the idea how to do that we first inspect what methods we can call on dictionary.
Here are the methods:
d={'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10}
Python Dictionary clear() Removes all Items
Python Dictionary copy() Returns Shallow Copy of a Dictionary
Python Dictionary fromkeys() Creates dictionary from given sequence
Python Dictionary get() Returns Value of The Key
Python Dictionary items() Returns view of dictionary (key, value) pair
Python Dictionary keys() Returns View Object of All Keys
Python Dictionary pop() Removes and returns element having given key
Python Dictionary popitem() Returns & Removes Element From Dictionary
Python Dictionary setdefault() Inserts Key With a Value if Key is not Present
Python Dictionary update() Updates the Dictionary
Python Dictionary values() Returns view of all values in dictionary
The brutal method to check if the key already exists may be the get() method:
d.get("key")
The other two interesting methods items() and keys() sounds like too much of work. So let's examine if get() is the right method for us. We have our dict d:
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10}
Printing shows the key we don't have will return None:
print(d.get('key')) #None
print(d.get('clear')) #0
print(d.get('copy')) #1
We use that to get the information if the key is present or no.
But consider this if we create a dict with a single key:None:
d= {'key':None}
print(d.get('key')) #None
print(d.get('key2')) #None
Leading that get() method is not reliable in case some values may be None.
This story should have a happier ending. If we use the in comparator:
print('key' in d) #True
print('key2' in d) #False
We get the correct results.
We may examine the Python byte code:
import dis
dis.dis("'key' in d")
# 1 0 LOAD_CONST 0 ('key')
# 2 LOAD_NAME 0 (d)
# 4 COMPARE_OP 6 (in)
# 6 RETURN_VALUE
dis.dis("d.get('key2')")
# 1 0 LOAD_NAME 0 (d)
# 2 LOAD_METHOD 1 (get)
# 4 LOAD_CONST 0 ('key2')
# 6 CALL_METHOD 1
# 8 RETURN_VALUE
This shows that in compare operator is not just more reliable, but even faster than get().
The ways in which you can get the results are:
if your_dict.has_key(key) Removed in Python 3
if key in your_dict
try/except block
Which is better is dependent on 3 things:
Does the dictionary 'normally has the key' or 'normally does not have the key'.
Do you intend to use conditions like if...else...elseif...else?
How big is dictionary?
Read More: http://paltman.com/try-except-performance-in-python-a-simple-test/
Use of try/block instead of 'in' or 'if':
try:
my_dict_of_items[key_i_want_to_check]
except KeyError:
# Do the operation you wanted to do for "key not present in dict".
else:
# Do the operation you wanted to do with "key present in dict."
Python 2 only: (and Python 2.7 supports `in` already)
You can use the has_key() method:
if dict.has_key('xyz')==1:
# Update the value for the key
else:
pass
Just an FYI adding to Chris. B's (best) answer:
d = defaultdict(int)
Works as well; the reason is that calling int() returns 0 which is what defaultdict does behind the scenes (when constructing a dictionary), hence the name "Factory Function" in the documentation.
A Python dictionary has the method called __contains__. This method will return True if the dictionary has the key, else it returns False.
>>> temp = {}
>>> help(temp.__contains__)
Help on built-in function __contains__:
__contains__(key, /) method of builtins.dict instance
True if D has a key k, else False.
Another way of checking if a key exists using Boolean operators:
d = {'a': 1, 'b':2}
keys = 'abcd'
for k in keys:
x = (k in d and 'blah') or 'boo'
print(x)
This returns
>>> blah
>>> blah
>>> boo
>>> boo
Explanation
First, you should know that in Python, 0, None, or objects with zero length evaluate to False. Everything else evaluates to True. Boolean operations are evaluated left to right and return the operand not True or False.
Let's see an example:
>>> 'Some string' or 1/0
'Some string'
>>>
Since 'Some string' evaluates to True, the rest of the or is not evaluated and there is no division by zero error raised.
But if we switch the order 1/0 is evaluated first and raises an exception:
>>> 1/0 or 'Some string'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>>
We can use this for pattern for checking if a key exists.
(k in d and 'blah')
does the same as
if k in d:
'blah'
else:
False
This already returns the correct result if the key exists, but we want it to print 'boo' when it doesn't. So, we take the result and or it with 'boo'
>>> False or 'boo'
'boo'
>>> 'blah' or 'boo'
'blah'
>>>
You can use a for loop to iterate over the dictionary and get the name of key you want to find in the dictionary. After that, check if it exist or not using if condition:
dic = {'first' : 12, 'second' : 123}
for each in dic:
if each == 'second':
print('the key exists and the corresponding value can be updated in the dictionary')

Python dictionary, lists ,join and Interpreter [duplicate]

This question already has answers here:
Why is the order in dictionaries and sets arbitrary?
(5 answers)
Closed 5 years ago.
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!.
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
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'

Initializing Python dictionary using dict() vs {} [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 7 years ago.
I am witnessing a very weird behviour with the following Python code snippet.
list_of_dict = [{}]*2 # create a list of 2 dict
list_of_dict[0]["add_to_dict_0"] = 1 # add a (key,val) pair to 1st dict
print list_of_dict[0]
print list_of_dict[1]
The output is:
{'add_to_dict_0': 1}
{'add_to_dict_0': 1}
Both the dictionaries got updated, although I (explicitly) updated only one. However, changing the initialization in Line 1 to the following:
list_of_dict = [dict() for _ in xrange(0,2)]
fixes the issue.
What is the reason for this behaviour? I am guessing that this is an artefact of the concept of name-object binding in Python (as opposed to variable-memory location binding in other languages like C/C++). My guess is that in the first case, both list_of_dict[0] and list_of_dict[1] are bound to the same dictionary object.
Is my suspicion correct? If yes, then why is the behaviour not replicated in the second case? What is the difference between dict_object = dict() and dict_object = {}?
There is no difference between dict_object = dict() and dict_object = {}
The problem here is [{}] * 2 will generate a list with 2 elements which are reference to a single object. [{} for x in range(2)] will generate a list with two elements which are reference two distinct objects.
Replacing {} with dict() will get the same result.
Both the dictionary in the list pointing to same object, Hence modifying one will will change others also
>>> list_of_dict = [{}]*2
>>> id(list_of_dict[0])
50030176
>>> id(list_of_dict[1])
50030176
When you created two empty dictionary by using dict(), dict() return different object every time
>>> d1 = dict()
>>> d2 = dict()
>>> id(d1)
50074656
>>> id(d2)
50074512

Categories

Resources