I have a dictionary: dict = {"key":[None, "item 2", "item 3"]}
How can I check that dict["key"][0] is None if “key” is unknown.
I have this so far:
{k: dict[k][0] for k in dict.viewkeys()}
which gives me:
{"key":None}
In my application, I am testing to see if dict["key"][0] has been populated yet, its default is None.
You’re very close. If you want all key, value pairs such that the first item in the value list is
None, you could do this:Note that you shouldn’t use
dictas a variable name because it masks thedictbuilt-in — even in example code, if only because it may mislead people.Once you’ve got
unassigned_items, you can simply test to see if'key'is inunassigned_items: