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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:08:49+00:00 2026-05-31T02:08:49+00:00

I often find myself using this construct: dict1[‘key1’] = dict2[‘key1’] dict1[‘key2’] = dict2[‘key2’] dict1[‘key3’]

  • 0

I often find myself using this construct:

dict1['key1'] = dict2['key1']
dict1['key2'] = dict2['key2']
dict1['key3'] = dict2['key3']

Kind of updating dict1 with a subset of dict2.

I think there is no a built method for doing the same thing in form

dict1.update_partial(dict2, ('key1', 'key2', 'key3'))

What approach you usually take? Have you made you own function for that? How it looks like?

Comments?


I have submitted an idea to python-ideas:

Sometimes you want a dict which is subset of another dict. It would nice if
dict.items accepted an optional list of keys to return. If no keys are
given – use default behavior – get all items.

class NewDict(dict):

    def items(self, keys=()):
        """Another version of dict.items() which accepts specific keys to use."""
        for key in keys or self.keys():
            yield key, self[key]


a = NewDict({
    1: 'one',
    2: 'two',
    3: 'three',
    4: 'four',
    5: 'five'
})

print(dict(a.items()))
print(dict(a.items((1, 3, 5))))

vic@ubuntu:~/Desktop$ python test.py 
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}
{1: 'one', 3: 'three', 5: 'five'}

So to update a dict with a part of another dict, you would use:

dict1.update(dict2.items(['key1', 'key2', 'key3']))
  • 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-31T02:08:50+00:00Added an answer on May 31, 2026 at 2:08 am

    There is no built-in function I know of, but this would be a simple 2-liner:

    for key in ('key1', 'key2', 'key3'):
        dict1[key] = dict2[key]  # assign dictionary items
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My PHP code is split between many files, and often I find myself using
I find myself using the current pattern quite often in my code nowadays var
Very often, I find myself using a callback function and I don't have its
I often find myself wanting to just print (using the implicit toString() of each
I often find myself wanting to collapse an n-dimensional matrix across one dimension using
I often find myself using lambdas as some sort of local functions to make
When programming in Stata I often find myself using the loop index in the
Ok, I find myself doing this often. I'll attach ids to my tables and
When using GTK# from C# on Mono, I often find myself copying out C#
I often find myself using several programming languages when making .NET applications. Usually C++/CLI

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.