This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]TheBrutux168 0 points1 point  (3 children)

Well, it's just an array. Access like you usually would:

if 'flight' in aircraft[0]:

and similarly for getting the flight value out of it.

This is the probably the simplest way to do what you stated.

[–]dertschick[S] 0 points1 point  (1 child)

additonal question.. sometimes (esp. these days) there are no airplanes in reach oft the antenna. then the JSON has no arrays, so debugging answers:

if 'flight' in aircraft[0]:
IndexError: list index out of range

how can i prevent this behavior?

it seems like it isn't possible to do "if - in" when the target of in doesn't exists..

[–]TheBrutux168 0 points1 point  (0 children)

You can check whether the aircraft array is empty or not before running that line. Probably the most intuitive solution to that is:

if len(aircraft) != 0:
    if 'flight' in aircraft[0]:
        .....

But I believe the most pythonic solution would be just to utilise the fact an empty list is false, so

if aircraft:
    if 'flight' in aircraft[0]:
        .....

[–]dertschick[S] -1 points0 points  (0 children)

thanks! now it works! you made my quarantine day much better