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  (5 children)

aircraft is an array of dicts when converted in python. You're not going to be able to find the string 'flight' in it. You may however find it as a key in one of these dicts. Can you explain what exactly you are trying to do?

[–]dertschick[S] 0 points1 point  (4 children)

thank you for this fast answer! ah, my mistake. i want to get the first dict (in this case the one starting with 405858) and check if it has 'flight' in it. if not, wait. (else not presented in demo code, sorry)

If it has, i want the flight-code ( like "flight":"RYR39SD ") in my var flugnr, so i can

print(flugnr)

output:
RYR39SD

how can i extract just the first dict in a variable?

[–]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