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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:11:00+00:00 2026-06-16T19:11:00+00:00

Short Version: In Python is there a way to (cleanly/elegantly) say Give me these

  • 0

Short Version:

In Python is there a way to (cleanly/elegantly) say “Give me these 5 (or however many) properties of an object, and nothing else, as a dictionary”?

Longer Version:

Using the Javascript Underscore library, I can reduce an bunch of objects/dictionaries (in JS they’re the same thing) to a bunch of subsets of their properties like so:

var subsets = _(someObjects).map(function(someObject) {
    _(someObject).pick(['a', 'd']);
});

If I want to do the same thing with a Python object (not a dictionary) however it seems like the best I can do is use a list comprehension and manually set each property:

subsets = [{"a": x.a, "d": x.d} for x in someObjects]

That doesn’t look so bad when there’s only two properties, and they’re both one letter, but it gets uglier fast if I start having more/longer properties (plus I feel wrong whenever I write a multi-line list comprehension). I could turn the whole thing in to a function that uses a for loop, but before I do that, is there any cool built-in Python utility thing that I can use to do this as cleanly (or even more cleanly) than the JS version?

  • 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-16T19:11:02+00:00Added an answer on June 16, 2026 at 7:11 pm

    This can be done simply by combining a list comprehension with a dictionary comprehension.

    subsets = [{attr: getattr(x, attr) for attr in ["a", "d"]} 
               for x in someObjects]
    

    Naturally, you could distill out that comprehension if you wanted to:

    def pick(*attrs):
        return {attr: getattr(x, attr) for attr in attrs} 
    
    subsets = [pick("a", "d") for x in someObjects]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short version: Is there way to achieve in Python the same effect achieved by
Short version: In pure Java EE 6, is there something like Spring's Authentication Processing
Short version: Is it easy/feasible/possible to program modal window in Flash (AS3)? Is there
Short version: I'm trying to determine the best way to track what the user
short version: how can I get rid of the multiple-versions-of-python nightmare ? long version:
Short version: What's the best way to override dict.keys() and friends to keep myself
There have been many questions along these lines but I'm struggling to apply them
Short version: Can I grant access to external databases to a role? Long version:
Short version : I would like the maven-glassfish-plugin to only be executed in the
Short Version: I'm looking for a technique to keep nearly-sorted data in nearly-sorted order

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.