0

i have a list that consists of different dicts and I iterate over them and get variables at certain places and if the variable does not exist I want to describe it with none. Something like this:

packet_time = packet[i].payload.time

And if time does not exist in payload, I want to add None as value of packet_time

  • Can you post the full relevant code to the body please? – Caleb Carson Mar 31 '23 at 13:46
  • 3
    You can use `dict.get(key, value)` (`value` is optional) to try to get a value from a dictionary, and return `value` if the key does not exist. By default, this value is `None`. In your case, you can do `packet_time = packet.get(i); if packet_time: packet_time = packet_time.payload.time`. – B Remmelzwaal Mar 31 '23 at 13:47
  • Does this answer your question? [Return a default value if a dictionary key is not available](https://stackoverflow.com/questions/6130768/return-a-default-value-if-a-dictionary-key-is-not-available) – JonSG Mar 31 '23 at 13:49

0 Answers0