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

  • Home
  • SEARCH
  • 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 6066223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:28:40+00:00 2026-05-23T09:28:40+00:00

I am working on a function. If source is found in d then it

  • 0

I am working on a function. If “source” is found in “d” then it will be added to it’s value of dictionary object, otherwise it will be added. For example in this case. “a” is added twice but “b” is added once.

I would like to get output as below(last line)

Thank you.

def adder(source,dest,weight):
   """"""


if __name__ == "__main__":
  d = {} #dictionary
  adder('a','b',1)
  adder('a','f',4)
  adder('b','c',1)
  adder('f','g',3)

  print d

  {'a':{'b':1,'f':4}, 'b':{'c':1}, 'f':{'g':3},g:{},c:{}} #<----final o/p needed
  • 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-05-23T09:28:41+00:00Added an answer on May 23, 2026 at 9:28 am

    The following implementation should do so:

    def adder(dict, source, dest, weight):
       if not source in dict:
         dict[source] = {}
       if not dest in dict:
         dict[dest] = {}
       dict[source][dest] = weight
    

    Please note that I added dict as first argument to your method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP (among others) will execute the deepest function first, working its way out. For
I am working on a function to establish the entropy of a distribution. It
I was just working on a function that I needed to return two values,
I'm working on creating a call back function for an ASP.NET cache item removal
I'm working with LINQ to objects and have a function where in some cases
Working on an old site in asp classic. I want to write a function
I'm currently working on a Google Maps project and am implementing a search function.
I've got a (mostly) working plugin developed, but since its function is directly related
I'm working on a fairly big open source RTS game engine ( Spring ).
I'm trying to write up a function which will post data (and append the

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.