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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:39:25+00:00 2026-06-07T09:39:25+00:00

I need to add a key with a value that increases by one for

  • 0

I need to add a key with a value that increases by one for every item in the nested dictionary. I have been trying to use the dict['key']='value' syntax but can’t get it to work for a nested dictionary. I’m sure it’s a very simple.

My Dictionary:

mydict={'a':{'result':[{'key1':'value1','key2':'value2'},
                        {'key1':'value3','key2':'value4'}]}}

This is the code that will add the key to the main part of the dictionary:

for x in range(len(mydict)):
        number = 1+x
        str(number)
        mydict[d'index']=number

print mydict
  #out: {d'index':d'1',d'a'{d'result':[...]}}

I want to add the new key and value to the small dictionaries inside the square parentheses:

{'a':{'result':[{'key1':'value1',...,'index':'number'}]}}

If I try adding more layers to the last line of the for loop I get a traceback error:

Traceback (most recent call last):
  File "C:\Python27\program.py", line 34, in <module>
    main()
  File "C:\Python27\program.py", line 23, in main
    mydict['a']['result']['index']=number
TypeError: list indices must be integers, not unicode

I’ve tried various different ways of listing the nested items but no joy. Can anyone help me out here?

  • 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-07T09:39:27+00:00Added an answer on June 7, 2026 at 9:39 am

    The problem is that mydict is not simply a collection of nested dictionaries. It contains a list as well. Breaking up the definition helps clarify the internal structure:

    dictlist = [{'key1':'value1','key2':'value2'},
                {'key1':'value3','key2':'value4'}]
    resultdict = {'result':dictlist}
    mydict = {'a':resultdict}
    

    So to access the innermost values, we have to do this. Working backwards:

    mydict['a'] 
    

    returns resultdict. Then this:

    mydict['a']['result']
    

    returns dictlist. Then this:

    mydict['a']['result'][0]
    

    returns the first item in dictlist. Finally, this:

    mydict['a']['result'][0]['key1']
    

    returns 'value1'

    So now you just have to amend your for loop to iterate correctly over mydict. There are probably better ways, but here’s a first approach:

    for inner_dict in mydict['a']['result']: # remember that this returns `dictlist`
        for key in inner_dict:
            do_something(inner_dict, key)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array that I need to add some key/value pairs too but
Hї! I have wrote test for my application. I need add item to database
I need to add key/value to queryset per each object based on stuff in
i need a Key/Value List with more than one Value per Key! What i
If you use RedBean ORM, do you need to add a primary key named
There are a multitude of key-value stores available. Currently you need to choose one
I have dictionary that is populated and I have no control of. I need
I need to add a unique key to a form in a hidden input,
Need to add two same name .csproj class libraries in my solution.Have two project
So I created a dictionary DICTIONARY1 dicFilesDT.Add(dateParts[1], (dateParts[1] + dateParts[2] )); so that I

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.