1

I have a dict created in a for loop in Python dict = {year:{month:{day:[title]}}} where year, month, day, and title are all variables. I then use data = json.dumps(dict) which works perfectly. But if the day is the same, I'd like it to add another [title] aspect to the array, so it would be

for title in x:
    dict = {year:{month:{day:[title]}}}
    data = json.dumps(dict)
if day==day:
    //insert another [title] right next to [title]

I've tried using append, update, and insert, but none of them work.

How would I go about doing this?

7
  • 1
    That's not an array. That's a dict. Even in JSON terminology, it's an object, not an array. Commented Jul 24, 2014 at 15:05
  • 1
    Then check and add the [title] before you dump it? Also as @user2357112 said, you are not making a Python list (JSON array), you're making a dict. (JSON object). Commented Jul 24, 2014 at 15:05
  • @CaseyFalk That makes sense, but how do I add it? Commented Jul 24, 2014 at 15:08
  • And what does "Title right next to title" mean? :/ Commented Jul 24, 2014 at 15:14
  • @user3822146, what do you mean "add"? What are you trying to get as a result? Commented Jul 24, 2014 at 15:18

1 Answer 1

3

Note that as user2357112 mentioned, you are creating a Python dict -- not a Python list (aka a JSON "array"). Thus, when you say "[title] right next to [title]" there is a bit of confusion. Dicts do not use the order you are expecting (they use a hash-ordering).

That, and you are attempting to add a field after you've dumped the JSON to a string. You should do that before you dump it. More so, you're throwing away both your dict and data variables every loop. As written, your code will only have access to the variables in the last iteration of the loop.

And another important note: don't overload dict. Rename your variable to something else.

Also, your line day==day will always return True...

Here is what I think you are trying to do: you are creating a "calendar" of sorts that is organized into years, then months, then days. Each day has a list of "titles."

# Variables I'm assuming exist:
# `title`, `year`, `month`, `day`, `someOtherDay`, `titles`, `someOtherTitle`

myDict = {}
for title in titles: #Renamed `x` to `titles` for clarity.
    # Make sure myDict has the necessary keys.
    if not myDict[year]:
        myDict[year] = {}
    if not myDict[year][month]: 
        myDict[year][month] = {}

    # Set the day to be a list with a single `title` (and possibly two).
    myDict[year][month][day] = [title]
    if day==someOtherDay:
        myDict[year][month][day].append(someotherTitle)

    # And FINALLY dump the result to a string.
    data = json.dumps(myDict) 
Sign up to request clarification or add additional context in comments.

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.