Reputation: 3643
i have a question about the following code
smaller={}
for( dest in a[neigbour].keys())
if(dest in smaller.keys() == False):
print 'false'
}
I can't make this code print false
.. am I doing something wrong? I wonder if I am doing the correct thing to check the statement dest in smaller.keys() == False
Upvotes: 0
Views: 250
Reputation: 142176
As well as the other answers you've been given, the code could be written as:
for key in a[neighbour].viewkeys() - smaller.viewkeys():
print key, 'not found'
Which takes advantage of the set like behaviour of .viewkeys
to easily create a set of all keys in a[neighbour]
not in b
, then loops over that.
Upvotes: 1
Reputation: 26050
Your Python syntax is quite confused. For one, you need a :
after your for
statement, and it's generally not idiomatic to use braces around a for
loop in Python. Also, instead of comparing to False
with ==
, generally we use not
:
smaller = {}
for dest in a[neighbour].keys():
if dest not in smaller.keys():
print('false')
Upvotes: 4
Reputation: 56915
The oppisite of dest in smaller.keys()
is dest not in smaller.keys()
. No need to compare to False
or True
:
if (dest not in smaller.keys()):
Documentation for in
and not in
: http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange
Upvotes: 4