Python Key Error=0 - Can't find Dict error in code
The error you're getting is that self.adj
doesn't already have a key 0
. You're trying to append to a list that doesn't exist yet.
Consider using a defaultdict
instead, replacing this line (in __init__
):
self.adj = {}
with this:
self.adj = defaultdict(list)
You'll need to import at the top:
from collections import defaultdict
Now rather than raise a KeyError
, self.adj[0].append(edge)
will create a list automatically to append to.
KeyError: 0 python with python dict
EDIT:
Fixed my error by creating a list which is appended each key to be removed which is then passed to this function:
def Aliens():
global aliens
clean = list()
for i in aliens.keys():
if(aliens[i][0] == (-100, -100) or aliens[i][1] < 0):
clean.append(i)
cleanAliens(clean)
and
def cleanAliens(cleanList):
global aliens
for i in range(len(cleanList)):
aliens.pop(cleanList[i]);
I'm getting Key error in python
A KeyError
generally means the key doesn't exist. So, are you sure the path
key exists?
From the official python docs:
exception KeyError
Raised when a mapping (dictionary) key is not found in the set of
existing keys.
For example:
>>> mydict = {'a':'1','b':'2'}
>>> mydict['a']
'1'
>>> mydict['c']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'c'
>>>
So, try to print the content of meta_entry
and check whether path
exists or not.
>>> mydict = {'a':'1','b':'2'}
>>> print mydict
{'a': '1', 'b': '2'}
Or, you can do:
>>> 'a' in mydict
True
>>> 'c' in mydict
False
Python KeyError: 0 when working with dictionary and functions
At the end of the function, result
should look like this: {'cro': X, 'eng': Y}
, where X and Y are numbers. I don't know what your dictionaries are, so I can't guess what the numbers are. Evaluating result['eng']
will produce a number, as will result['cro']
, but there is no 0
key in this dictionary.
Further, the second indexing operation will also give you issues. result['eng'][0]
will give you an error because result['eng']
is a number, and you can't index into a number.
What do you expect the output of this function to look like? Where is sort
defined and what is it supposed to do?
Related Topics
How to Install Tesseract for Python on Anaconda
How to Split a CSV File Row to Columns in Python
How to Install Pypdf2 Module Using Windows
How to Write Python Array (Data = []) to Excel
Python3: Remove a Substring Between Two Delimiting Char
Replace Empty Strings With None/Null Values in Dataframe
How to Update Sqlalchemy Orm Object by a Python Dict
Typeerror: Missing 1 Required Positional Argument: 'Self'
How to Locate the Index With in a Nested List Python
How to Create a for Loop That Goes Through All Diagonal Possibilities of a List
Python Overflowerror: Int Too Large to Convert to Float
Generate List of Quarters Betweeen Given Dates
How to Connect to a Remote Windows Machine to Execute Commands Using Python
Spliting a Row to Multiple Row Pyspark
How to Locate Elements on Webpage With Headless Chrome
Pythone :How to Use Dataframe Output in Email Body as Text
Join Dataframes Based on Partial String-Match Between Columns