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 8111599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:11:04+00:00 2026-06-06T02:11:04+00:00

When one is not using methods, what is the best, way to achieve the

  • 0

When one is not using methods, what is the best, way to achieve the following.

  1. have a single function for adding items to a dictionary
  2. use that function when adding multiple items to the dictionary
  3. Does this make everything super slow?

The following works, I am not very serious about using it, but am interested in what is possible.

Simple functions to create, and update a dictionary.

from collections import defaultdict
from functools import partial

def addfoo(mydict, foo):
    x,y,z = foo
    mydict[x].add((y,z))
    mydict[y].add((x,z))
    return mydict

makemydict = partial(addfoo, defaultdict(set))

foo = ('foo','bar',1)

mydict = makemydict(foo)

print(mydict)
# defaultdict(<class 'set'>, {'foo': {('bar', 1)}, 'bar': {('foo', 1)}})

While the next part of this works, I was wondering if there was a way to use map, to run the function over the ‘foos’ (plural). but as the addfoo function returns a value, how could that be mapped over. The following works, but are there any other ways?

def addfoos(mydict, foos):
    for foo in foos:
        mydict = addfoo(mydict,foo)
    return mydict

makemany = partial(addfoos, defaultdict(set))

foos = { ('foo','bar',1),
         ('bar','baz',0) }

mydict = makemany(foos)

print(mydict)
# defaultdict(<class 'set'>, {'baz': {('bar', 0)}, 'foo': {('bar', 1)}, 'bar': {('foo', 1), ('baz', 0)}})

As it is, it works… but I am curious if python has a fmap or similar.


OK… for anyone stumbling across this — use reduce!

the following, although with different variables is exactly what I was aiming for:

from collections import defaultdict
from functools import partial
from functools import reduce

def addnode(graph, node):
    x,y,z = node
    graph[x].add((y,z))
    graph[y].add((x,z))
    return graph

def addnodes(graph, nodes):
    return reduce(addnode, nodes, graph)

graphnode  = partial(addnode, defaultdict(set))
graphnodes = partial(addnodes, defaultdict(set))

and now, either function (addnode or addnodes) can be called to update the dict.

  • 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-06T02:11:06+00:00Added an answer on June 6, 2026 at 2:11 am

    You want reduce, not map, since you’re reducing the inputs to a single result, rather than transforming them one-to-one to a list of results.

    >>> reduce(addfoo, foos, defaultdict(set))
    defaultdict(<type 'set'>, {'baz': set([('bar', 0)]), 'foo': set([('bar', 1)]), 'bar': set([('foo', 1), ('baz', 0)])})
    

    That was python 2.6. Python3 version works the same, but reduce is in functools now:

    >>> from functools import reduce
    >>> reduce(addfoo, foos, defaultdict(set))
    defaultdict(<class 'set'>, {'baz': {('bar', 0)}, 'foo': {('bar', 1)}, 'bar': {('foo', 1), ('baz', 0)}})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Coda slider on one webpage (hand-crafted, not using the plugin, but
I've two TableViews (one SourceList and one Normal TableView). I'm not using ArrayControllers, just
Using css (not a inline one, but external stylesheet) how do i override the
I would like to multi-thread an application, however one library i'm using is not
What tasks should one not use HBase for? My understanding is that HBase and
Can a function in R return not one, but two vectors? (I am really
I'm looking for the best practice way to achieve a message / notification system.
I was reading how to implement private methods in Objective-C ( Best way to
Is there a way in Python, to have more than one constructor or more
file.each_line do |line| #skip the first one/not a user 3.times { next } if

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.