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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:29:16+00:00 2026-06-06T18:29:16+00:00

I have a dictionary: (key is ‘name’ and value is other dictionary) dictionary =

  • 0

I have a dictionary: (key is ‘name’ and value is other dictionary)

dictionary = {'subnet 10.5.5.0 netmask 255.255.255.224': 
                         {'option-domain-name': '"internal.example.org"',
                         'option-domain-name-servers': 'ns1.internal.example.org',
                         'range': '10.5.5.26 10.5.5.30', 'max-lease-time': '7200', 
                         'default-lease-time': '600', 'option-routers': '10.5.5.1',
                         'option-broadcast-address': '10.5.5.31'},
              'subnet 10.254.239.32 netmask 255.255.255.224': 
                         {'range': 'dynamic-bootp 10.254.239.40 10.254.239.60',
                          'option-routers': 'rtr-239-32-1.example.org', 
                          'option-broadcast-address': '10.254.239.31'}, 
              'subnet 10.254.239.0 netmask 255.255.255.224': 
                         {'range': '10.254.239.10 10.254.239.20',
                          'option-routers': 'rtr-239-0-1.example.org, rtr-239-0-     
                           2.example.org'}, 
              'host fantasia': {'hardware': 'ethernet 08:00:07:26:c0:a5', 
                                'fixed-address': 'fantasia.fugue.com'}, 
              'host passacaglia': {'hardware': 'ethernet 0:0:c0:5d:bd:95',
                                   'server-name': '"toccata.fugue.com"',
                                   'filename': '"vmunix.passacaglia"'}
         }

All i have to do is change every
‘option-‘ for ‘option ‘

dictionary = {'subnet 10.5.5.0 netmask 255.255.255.224': {'option domain-name': '"internal.example.org"', 'option domain-name-servers': 'ns1.internal.example.org', 'range': '10.5.5.26 10.5.5.30', 'max-lease-time': '7200', 'default-lease-time': '600', 'option routers': '10.5.5.1', 'option broadcast-address': '10.5.5.31'}, 'subnet 10.254.239.32 netmask 255.255.255.224': {'range': 'dynamic-bootp 10.254.239.40 10.254.239.60', 'option routers': 'rtr-239-32-1.example.org', 'option broadcast-address': '10.254.239.31'}, 'subnet 10.254.239.0 netmask 255.255.255.224': {'range': '10.254.239.10 10.254.239.20', 'option routers': 'rtr-239-0-1.example.org, rtr-239-0-2.example.org'}, 'host fantasia': {'hardware': 'ethernet 08:00:07:26:c0:a5', 'fixed-address': 'fantasia.fugue.com'}, 'host passacaglia': {'hardware': 'ethernet 0:0:c0:5d:bd:95', 'server-name': '"toccata.fugue.com"', 'filename': '"vmunix.passacaglia"'}}

How do I do it?

  • 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-06T18:29:17+00:00Added an answer on June 6, 2026 at 6:29 pm
    for subdict in dictionary.values():
        for key, val in subdict.items():
            if key.startswith('option-'):
                del subdict[key]
                subdict[' '.join(key.split('-', 1))] = val
    

    So, for every contained dict, loop over it’s items, and if the key starts with option-, delete the key from the dictionary, and store the value under a new key with the dash removed.

    Demo:

    >>> from pprint import pprint
    >>> dictionary = {'subnet 10.5.5.0 netmask 255.255.255.224': {'option-domain-name': '"internal.example.org"', 'option-domain-name-servers': 'ns1.internal.example.org', 'range': '10.5.5.26 10.5.5.30', 'max-lease-time': '7200', 'default-lease-time': '600', 'option-routers': '10.5.5.1', 'option-broadcast-address': '10.5.5.31'}, 'subnet 10.254.239.32 netmask 255.255.255.224': {'range': 'dynamic-bootp 10.254.239.40 10.254.239.60', 'option-routers': 'rtr-239-32-1.example.org', 'option-broadcast-address': '10.254.239.31'}, 'subnet 10.254.239.0 netmask 255.255.255.224': {'range': '10.254.239.10 10.254.239.20', 'option-routers': 'rtr-239-0-1.example.org, rtr-239-0-2.example.org'}, 'host fantasia': {'hardware': 'ethernet 08:00:07:26:c0:a5', 'fixed-address': 'fantasia.fugue.com'}, 'host passacaglia': {'hardware': 'ethernet 0:0:c0:5d:bd:95', 'server-name': '"toccata.fugue.com"', 'filename': '"vmunix.passacaglia"'}}
    >>> for subdict in dictionary.values():
    ...     for key, val in subdict.items():
    ...         if key.startswith('option-'):
    ...             del subdict[key]
    ...             subdict[' '.join(key.split('-', 1))] = val
    ... 
    >>> pprint(dictionary)
    {'host fantasia': {'fixed-address': 'fantasia.fugue.com',
                       'hardware': 'ethernet 08:00:07:26:c0:a5'},
     'host passacaglia': {'filename': '"vmunix.passacaglia"',
                          'hardware': 'ethernet 0:0:c0:5d:bd:95',
                          'server-name': '"toccata.fugue.com"'},
     'subnet 10.254.239.0 netmask 255.255.255.224': {'option routers': 'rtr-239-0-1.example.org, rtr-239-0-2.example.org',
                                                     'range': '10.254.239.10 10.254.239.20'},
     'subnet 10.254.239.32 netmask 255.255.255.224': {'option broadcast-address': '10.254.239.31',
                                                      'option routers': 'rtr-239-32-1.example.org',
                                                      'range': 'dynamic-bootp 10.254.239.40 10.254.239.60'},
     'subnet 10.5.5.0 netmask 255.255.255.224': {'default-lease-time': '600',
                                                 'max-lease-time': '7200',
                                                 'option broadcast-address': '10.5.5.31',
                                                 'option domain-name': '"internal.example.org"',
                                                 'option domain-name-servers': 'ns1.internal.example.org',
                                                 'option routers': '10.5.5.1',
                                                 'range': '10.5.5.26 10.5.5.30'}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary with key-value pairs like {a : (b,c,d,e)} . If i
I have a Dictionary where the key and value are both strings. It's possible
Does VBA have dictionary structure? Like key<>value array?
I have a sorted dictionary where the key is a date and the value
I have a dictionary (see below) that has Key strings and Value strings and
I have a Dictionary with key of type UInteger and the value is List(Of
i have a dictionary where the key is a date and the value is
I have Dictionary from string key i want to get Value of corresponding key
I have a dictionary with each key containing a list as a value. And
I have a dictionary with a key value pair and need to loop through

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.