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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:36:32+00:00 2026-06-18T22:36:32+00:00

How might one map a function over certain values in a dictionary and also

  • 0

How might one map a function over certain values in a dictionary and also update those values in the dictionary?

dic1 = { 1 : [1, 2, 3, 4], 2 : [2, 3, 5, 5], 3 : [6, 3, 7, 2] ... }
map(func, (data[col] for data in dic1.itervalues()))

This is sort of what I’m looking for, but I need a way to reinsert the new func(val) back into each respective slot in the dict. The function works fine, and printed it returns all the proper index values with the func applied, but I can’t think of a good way to update the dictionary. Any ideas?

  • 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-18T22:36:33+00:00Added an answer on June 18, 2026 at 10:36 pm

    You don’t want to use map for updating any kind of sequence; that’s not what it’s for. map is for generating a new sequence:

    dict2 = dict(map(func, dict1.iteritems()))
    

    Of course func has to take a (key, old_value) and return (key, new_value) for this to work as-is. If it just returns, say, new_value, you need to wrap it up in some way. But at that point, you’re probably better off with a dictionary comprehension than a map call and a dict constructor:

    dict2 = {key: func(value) for key, value in dict1.itervalues()}
    

    If you want to use map, and you want to mutate, you could do it by creating a function that updates things, like this:

    def func_wrapped(d, key):
        d[key] = func(d[key])
    map(partial(func_wrapped, d), dict1)
    

    (This could even be done as a one-liner by using partial with d.__setitem__ if you really wanted.)

    But that’s a silly thing to do. For one thing, it means you’re building a list of values just to throw them away. (Or, in Python 3, you’re not actually doing anything unless you write some code that iterates over the map iterator.) But more importantly, you’re making things harder for yourself for no good reason. If you don’t need to modify things in-place, don’t do it. If you do need to modify things in place, use a for loop.

    PS, I’m not saying this was a silly question to ask. There are other languages that do have map-like mutating functions, so it wouldn’t be a silly thing to do in, say, C++. It’s just not pythonic, and you wouldn’t know that without asking.

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

Sidebar

Related Questions

How might one craft a good explanation of why the following code is not
How might one retrieve the last element of a string array in visual basic
In PHP, how might one sort an array by the last couple of characters
This one might be a little confusing. I'm using AMCharts with rails. Amcharts comes
In C++ one might use namespace to keep independent groups working in the same
I'm searching for strategies one might use to programmatically find files which may be
I find it not so easy as one might think to start with TDD
Sometimes in an application, one might compare the Message text of an exception. For
In the following inline conditionals, one might expect an integer and a double to
I'm not very expert on how processors work, but one might imagine that it

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.