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.
Related Posts:
- Understanding the main method of python [duplicate]
- TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- Python Math – TypeError: ‘NoneType’ object is not subscriptable
- PermissionError: [Errno 13] in Python
- “If not” condition statement in python
- syntaxerror: “unexpected character after line continuation character in python” math
- How can I sort a dictionary by key?
- Python Logic of ListNode in Leetcode
- How can I write a `try`/`except` block that catches all exceptions?
- Plot logarithmic axes with matplotlib in python
- Error in Python script “Expected 2D array, got 1D array instead:”?
- Relative imports for the billionth time
- How do I use the yfinance API in Python to get today’s DJI open?
- what does the __file__ variable mean/do?
- Pythonic way to create a long multi-line string
- Proper shebang for Python script
- ‘str’ object has no attribute ‘decode’. Python 3 error?
- How do I correctly clean up a Python object?
- Log to the base 2 in python
- Implement touch using Python?
- Python setup.py develop vs install
- keyerror 1 in my code
- How to clamp an integer to some range?
- How to extract the substring between two markers?
- Is there an operation for not less than or not greater than in python?
- Simple prime number generator in Python
- Background color for Tk in Python
- If Python is interpreted, what are .pyc files?
- How do I get monitor resolution in Python?
- What is runtime in context of Python? What does it consist of?
- Unable to convert 3d ply file image to 2d image
- Python High Pass Filter
- Where are the ampersand and vertical bar characters used in Python?
- python-How to solve KeyError: 2?
- Python: What OS am I running on?
- How to draw a line with matplotlib?
- What is the maximum float in Python?
- Get all object attributes in Python?
- Normalizing a list of numbers in Python
- mport win32ui in python 3.6
- KeyError: ‘plotly_domain’
- What is the purpose and use of **kwargs? [duplicate]
- How can I run python in terminal on a mac?
- ‘git’ is not recognized as an internal or external command
- Convert a tensor to numpy array in Tensorflow?
- How to update multiple columns in single update statement in DB2
- pandas: merge (join) two data frames on multiple columns
- Why do people write #!/usr/bin/env python on the first line of a Python script?
- How do I parallelize a simple Python loop?
- Python Save to file
- Pytorch tensor to numpy array
- ‘numpy.ndarray’ object has no attribute ‘index’
- ‘numpy.float64’ object is not iterable
- Difference between BeautifulSoup and Scrapy crawler?
- Python error – IDLE’s subprocess didn’t make connection. Either IDLE can’t start or personal firewall software is blocking connection
- Displaying better error message than “No JSON object could be decoded”
- Running Bash commands in Python
- Escape string Python for MySQL
- git submodule add error: does not have a commit checked out
- Effect of using sys.path.insert(0, path) and sys.path(append) when loading modules
- What is the best way to call a script from another script?
- Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings
- Python map object is not subscriptable
- Why does pycharm propose to change method to static
- Android – Package Name convention
- How to execute an oracle stored procedure?
- append new row to old csv file python
- In Flask, what is “request.args” and how is it used?
- How do I determine the size of an object in Python?
- Python function pointer
- How can I change the font size using seaborn FacetGrid?
- How to throw error and exit with a custom message in python
- Python a &= b meaning?
- File “/usr/bin/pip”, line 9, in
from pip import main ImportError: cannot import name main - Merge posts plugin? [closed]
- Slow WP Site, theme.php not caching? Up to 25 second load time! [closed]
- Redirect, Change URLs or Redirect HTTP to HTTPS in Apache – Everything You Ever Wanted to Know About mod_rewrite Rules but Were Afraid to Ask
- List all DNS records in a domain using dig?
- How to add dependency on a Windows Service AFTER the service is installed
- Monday morning mistake: sudo rm -rf –no-preserve-root /
- What’s the meaning of ‘@’ in a DNS zone file?
- Delete Amazon EC2 terminated instance
- robocopy transfer file and not folder
- What’s the difference between include_tasks and import_tasks?
- rm on a directory with millions of files
- What ports to open for mail server?
- Why is Enterprise Storage so expensive?
- Should a wildcard SSL certificate secure both the root domain as well as the sub-domains?
- Why can’t MX records point to an IP address?
- How to show all banned IP with fail2ban?
- How can I get diff to show only added and deleted lines? If diff can’t do it, what tool can?
- Adding a user to an additional group using ansible
- Best system administrator accident [closed]
- Reread partition table without rebooting?
- Storing a million images in the filesystem
- How to keep rsync from chown’ing transferred files?
- How to view connected users to open vpn server?
- Can I specify a port in an entry in my /etc/hosts on OS X? [duplicate]
- How to check for modified config files on a Debian system?