Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9154449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:25:50+00:00 2026-06-17T12:25:50+00:00

I have a simple nested dict to indicate a graph with nodes, vertices, and

  • 0

I have a simple nested dict to indicate a graph with nodes, vertices, and edge weights in the form dict:

{node: {vertex: weight, ... } ...}.

Here is how I create it:

with open(file) as f:
        __, __1 = next(f).split()
        for line in f:
            tail, head, weight = line.split()
                g1[tail] = {}
                g1[tail][head] = int(weight)

This code gives me the dict I want. However, when I run more code that creates a similar nested dict as well as accesses this dict, I run into an error. Here is the code:

nodes = g1.keys()
distance = {}

    for n in nodes:
        distance[n] = {}
    for k in nodes:
        distance[n][k] = graph[n][k]

Somewhere in here, this error crops up:

distance[n][k] = graph[n][k]
KeyError: '344'

indicating that I haven’t yet created a key for ‘344,’ which is what this code is trying to do. I thought by initializing each node n to a dict, I could then create an entry for distance[n][k]. I’ve tried using default dicts — the result is the same. Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:25:51+00:00Added an answer on June 17, 2026 at 12:25 pm
    for n in nodes:
        distance[n] = {}
    

    After this for-loop completes, n is equal to the last value in nodes.

    There is no guarantee that for every k in nodes, graph[n][k] exists:

    for k in nodes:
        distance[n][k] = graph[n][k]
    

    The nodes are all tails, not heads. So k is iterating over tails. Yet graph[n][k] is placing k in the position expected for a head.

    The tails and the heads are not necessarily interchangeable, and the last tail, n, may not be connected to every other tail, k.


    If you are trying to make distance a copy of graph, then use

    import copy
    distance = copy.deepcopy(graph)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple nested form using the 'nested_form' gem. It looks like this:
I have a simple nested form and I am setting a nested attribute with
I have a simple structure in mongodb, with nested array. How can I update
I have simple php validation form that is halfway working. If you leave the
I have a simple set of images nested in a div tag, I'm just
So I have a simple application as in RailsCast about nested forms. and a
I have a nested simple XML structure that I load with PHP's simpleXML. Some
I have a simple UIWebView nested in a ViewController it is presented modally off
I have a simple child div nested inside a parent div, like so... I
I have a simple situation that does not work. Gridview is nested inside updatepanel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.