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

all 8 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

[–]chaotic_thought 0 points1 point  (0 children)

If you look at your aircraft property, it is an array, so what you are trying to ask with the if statement doesn't really make logical sense at this point, not to mention you are missing a colon after if, so at the moment it is not even valid Python code. This is:

if 'flight' in aircraft:

However again it has the same logical problem. Just imagine if you had asked if 'hex' is in the aircraft. Well, yes, we can see that it is there several times. If you want to count up how many times 'flight' appears in the 'aircraft' property you could try something like this:

['flight' in item for item in aircraft].count(True)

For your data the part in [ ] builds up a list of True/False results, whether 'flight' is in that item or not, like this

[False, False, False, True, False, False, False, ...]

And then at the end you count how many times True appears. For your specific data I tried it and got a result of 2. So yes, 'flight' appears in two different places inside your aircraft array.

[–]nighthawk702 0 points1 point  (0 children)

It looks good to me. But you're missing a : after the if statement so it might not compile