0

I have a dictionary that contains a list capex in which there is one value. The basic structure of that dictionary is:

outputDic = {
    "capex": [{
                "value": 450000,
                "start": 5,
                "year": 10
     }]
}

value, start, and year are just the random numbers but there is decider variable which will basically decide how many values will be there in the list. So let just say if decider is 2 then there will two values and if it is 3 then there will be three values in the list and so on.My approach:

outputDic = {
    "capex": [{
               "value": 450000,
               "start": 5,
               "year": 10
           }]
}

decider = 2
for i in range(1, decider):
    outputDic['capex'][i]['value'+str(i)] = 40*i
    outputDic['capex'][i]['start'+str(i)] = 20*i
    outputDic['capex'][i]['year'+str(i)] = 10*i
    
print(outputDic)

It gives an error of List out of index and that may be because there is only one element in the list. My expected output is:

decider = 2
outputDic = {
    "capex": [
        {
            "value": 40,
            "start": 20,
            "year": 10
        },
        {
            "value": 80,
            "start": 40,
            "year": 20
        }
    ]
}
Can anyone please help?
4
  • 2
    capex is a list, you're trying to assign to an index that does not exist which is why you get an error. Try appending a dictonnary directly to the list. Commented Jul 20, 2020 at 7:51
  • 2
    "and that may be because there is only one element in the list" yes, exactly. Are you asking how to add items to a list? Commented Jul 20, 2020 at 7:51
  • yes I am asking to add value, start and year acc to decider Commented Jul 20, 2020 at 8:00
  • As you can see in the expected output section where I explained if the decider is 2 the there will be two values in the Capex list Commented Jul 20, 2020 at 8:02

1 Answer 1

2

Here's a way to do that. Use append to add new items to the list.

outputDic = {
        "capex": []
}
decider = 2
for i in range(decider):
    outputDic['capex'].append(
        {
            'value' : 40*i, 
            'start': 20*i, 
            'year':10*i
        }
    )

print(outputDic)

The output is:

{'capex': [{'value': 0, 'start': 0, 'year': 0}, 
           {'value': 40, 'start': 20, 'year': 10}]}
Sign up to request clarification or add additional context in comments.

3 Comments

hey, thanks for the reply but there is one problem with this. Its not changing the default value. In the original dic the value, start and year remain same
Would you like the first entry in the dict to change as well, according to the same logic?
ahh, I am sorry but there is still one small issue with this. the name of the value, start and year should remain the same. Can we do that ? I mean it should be value, start and year just, not value0, start0, year0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.