1

Hi I'm trying to create nested dictionary which is supposed to look like:

{'4/1': {'A': 5, 'B': 6},  '4/2': {'A': 1, , 'B': 7}, '4/3': {'A': 4, 'B': 3}}

What I'm getting is:

{'4/1': {'A': 5}, '4/2': {'A': 1}, '4/3': {'A': 4}}

Code:

import random
import datetime as dt

futuresIncome = {}
incomeTypes = ["A", "B"]

for incomeType in incomeTypes:
    for month in range(4, 5):
        for day in range(1, 4):
            date = str(month) + "/" + str(day)
            
            if date in futuresIncome.keys():
                continue
            else:
                futuresIncome[date] = {}
                futuresIncome[date][incomeType] = random.randint(1, 11)

What am I doing wrong? Thank you.

2
  • 1
    In your outer loop, you add a key for each date for "A", then on the next iteration (B) you then check if the key is there (which it is) and then continue, so your second iteration doesn't do anything. Try use pdb to debug this, you'll see that you always hit the continue path once you get to the B iteration. Commented Apr 7, 2021 at 13:47
  • also, instead of futuresIncome[date] = {}, consider using a defaultdict(dict) instead. And you can change date = str(month) + "/" + str(day) to date = f"{month}/{day}" Commented Apr 7, 2021 at 13:48

4 Answers 4

2

You need to use defaultdict:

import random
import datetime as dt
from collections import defaultdict

futuresIncome = defaultdict(dict)
incomeTypes = ["A", "B"]

for incomeType in incomeTypes:
    for month in range(4, 5):
        for day in range(1, 4):
            date = str(month) + "/" + str(day)
            futuresIncome[date][incomeType] = random.randint(1, 11)
            
Sign up to request clarification or add additional context in comments.

Comments

1

You should not continue in the if block but add the data for the current date:

if date in futuresIncome.keys():
    futuresIncome[date][incomeType] = random.randint(1, 11)

You could also make use of the setdefault() method and get rid of the if-else block:

for incomeType in incomeTypes:
    for month in range(4, 5):
        for day in range(1, 4):
            date = str(month) + "/" + str(day)
            futuresIncome.setdefault(date, {}).update({incomeType: random.randint(1, 11)})

Comments

0
            if date in futuresIncome.keys():
                continue

This part causes that

            else:
                futuresIncome[date] = {}
                futuresIncome[date][incomeType] = random.randint(1, 11)

will be executed only once if futuresIncome doesn't contain this date.

Probably you wanted to do something like that:

            if date in futuresIncome.keys():
                futuresIncome[date] = {}
            futuresIncome[date][incomeType] = random.randint(1, 11)

Generally, better option than that would be defaultdict.

Comments

0

I will suggest iterating through incomeTypes in the innermost loop as shown below: `

import random

import datetime as dt

futuresIncome = {}

incomeTypes = ["A", "B"]


for month in range(4, 5):

    for day in range(1, 4):

         for incomeType in incomeTypes:
            date = str(month) + "/" + str(day)
            
            if date in futuresIncome.keys():
                continue
            else:
                futuresIncome[date] = {}
                futuresIncome[date][incomeType] = random.randint(1, 11)`

Comments

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.