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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:29:27+00:00 2026-06-09T21:29:27+00:00

Given the following data structure: out = { ‘foo’: { ‘public’:{}, ‘private’:{}, ‘other’:{} },

  • 0

Given the following data structure:

out = {
  'foo': { 'public':{}, 'private':{}, 'other':{} },
  'bar': { 'public':{}, 'private':{}, 'other':{} }
}

I am attempting to slice out parts of the sub-structure to create a new dict. My use for this is to respond to requests with all data except that marked private.

To do the opposite is trivial:

response = {x,y['private'] for x,y in out.iteritems()}

Which constructs a dict for each foo and bar containing only the data marked private. But is there some functionality in the standard library (itertools perhaps) that would produce the following:

out = {
  'foo': { 'public':{}, 'other':{} },
  'bar': { 'public':{}, 'other':{} }
}

I have tried the following:

{x:(y['public'], y['other']) for x,y in out.iteritems()}

Although I would prefer to not use a tuple, and not explicitly name each sub-structure, as this is not reusable or scalable.

def remove(name, obj):
    return {x:y for x,y in obj.iteritems() if x is not name}
{x:remove('private',y) for x,y in out.iteritems()}

This seems to work, but is there a better way? 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-09T21:29:29+00:00Added an answer on June 9, 2026 at 9:29 pm

    You can break this down into parts; you want a new dictionary which has some parts removed. So create a function which can return a dictionary without the elements in question and call this is part of an iterator.

    You’re using dictionary comprehensions so something like this would work:

    def remove_items(d, *items):
        """
        Return dictionary copy with some items removed.
        """
        return { a: b for a, b in d.iteritems() if a not in items }
    
    print { x: remove_items(y, 'private') for x, y in out.iteritems() }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an odd linq subquery issue. Given the following data structure: Parents Children
Given the following tree structure in Haskell: data Tree = Leaf Int | Node
Given the following XML structure: <root> <data>x</data> <details> <some_other_element>...</some_other_element> <collection> <element><a>1</a></element> <element><a>2</a></element> <element><a>3</a></element> <collection>
Given the following data structure var things = [{ name: thing1, sex: male}, {
Given the following data/schema: DECLARE @t1 TABLE ( Id int NOT NULL ) DECLARE
Given the following JSON data: [ { pk: 2, model: corkboard.announcement, fields: { body:
Given the following schema / data / output how would I format a SQL
Given the following example data: Users +--------------------------------------------------+ | ID | First Name | Last
Given the following table and data. create table prices (productKey int ,PriceType char(10) ,BeginDate
Given a simple table with the following data: id | result | played ----+--------+------------

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.