So I have this.
[{"what": "Drink", "allDay": true, "id": 12, "when": "2020-05-13", "end": "2020-05-13"}]
I'm trying to print the value of "what", but I'm having trouble converting this list into dictionary. I simply need the brackets come off. How do I do this? Thank you. :)
What you have is a list of dict(s). So, you could do it as follows.
values
is the name of list variable, then values[i]
gives you access to a dictionary stored at index i
.dict.get(key, None)
will either return a value or None when the key does not exist. This may allow you to avoid unwanted error when a certain field is absent.# Use of dict.get(key, "value-when-no-match")
# is safer than directly calling dict[key]
# BUT, this depends on your design/logic/requirements.
values[0].get('what', None)
Since "allDay": true
was in your sample data, I assume that most likely you are trying to read this from a JSON file. If that is the case, and if you are using json
library in python, that could be easily dealt with using json.load(), json.loads()
.
Note that you had a boolean value (for "allDay"
) as true
>> Python expects True
with a capital T.
values = [
{
"what": "Drink",
"allDay": True,
"id": 12,
"when": "2020-05-13",
"end": "2020-05-13"
},
{
"what": "Eat",
"allDay": True,
"id": 14,
"when": "2020-05-14",
"end": "2020-05-14"
}
]
Collected from the Internet
Please contact javaer1[email protected] to delete if infringement.
Comments