rand03
rand03

Reputation: 41

Can a dictionary key have multiple values?

I'm trying to organize my data in a way where it looks like this:

    { 'Month' = [
         'Day1' : 8
         'Day2' : 6
         'Day3' : 9
         'Day4' : 1
         'Day5' : 2
       ]
    }

In a way, I'm trying to have it be a dictionary where the months are the keys and the days are the values for the month. For the sake of this example, we're going to pretend that the numbers listed above for each day represents how many photos I've taken in my camera for that day.

Is this possible to do?

Upvotes: 0

Views: 120

Answers (2)

MattDMo
MattDMo

Reputation: 102852

Yes it is possible, like so:

    { 'Month': {
         'Day1': 8,
         'Day2': 6,
         'Day3': 9,
         'Day4': 1,
         'Day5': 2
       }
    }

The value of a dict key can be any type of collection.

Upvotes: 1

Matiiss
Matiiss

Reputation: 6156

something like this:

{'Month': {
     'Day1': 8,
     'Day2': 6,
     'Day3': 9,
     'Day4': 1,
     'Day5': 2
    }
}

just nest dictionaries (also there is no space between key and colon but there is space between value and colon, it is a PEP8 violation: PEP 8: E203 whitespace before ':' and PEP 8: E231 missing whitespace after ':' and this too PEP 8: E201 whitespace after '{')

Upvotes: 2

Related Questions