Reputation: 45
For example, say I have
node = {}
node['data'] = ['hi', '8']
newNode = {}
newNode['data'] = ['hello', '6']
and I want to compare the numbers 6 and 8 in node and newNode
if I try doing
print(node[1])
because the numbers are in position 1 of the lists I get an error that says:
KeyError: 1
Upvotes: 0
Views: 722
Reputation: 33764
By printing node[1]
, you're actually searching for a key named 1
inside your node dictionary. Instead, since you named it 'data', use node['data'][1]
. node['data']
refers to ['hi', '8']
. The following prints True of False for if 8 and 6 is the same.
node = {}
node['data'] = ['hi', '8']
# you can also create the dictionary by doing this:
# node = {'data' : ['hi', '8']}
# or
# node = dict{'data' = ['hi', '8']}
newNode = {}
newNode['data'] = ['hello', '6']
# so to compare:
print(node['data'][1]==newNode['data'][1])
Upvotes: 1