0

I am using below code, if i am sending "89" as pressure value and temperature as "Null" in this case it is showing "value not processed". I want it to show message of value processed and also for value not processed what condition i can use in below code.

    payload = [{'id': 'Room1',
    'pressure': {'metadata': {}, 'type': 'Number', 'value': '89'},
    'temperature': {'metadata': {}, 'type': 'Number', 'value': 'Null'},`'type': 'RoomTest'}]`
    attrs = ['temperature', 'pressure']
    x = (len(payload))
    Flag=True
    for i in range(x):
        for j in attrs:
        y = payload[i][j]['value']
        if '' in y or 'Null' in y:
          Flag=False
     if Flag:
         print("successfully processed")
     else:
         print("successfully not processed")

Any help will be appreciated.Thanks

2
  • 9
    if '' in y will always be true for any string Commented Nov 13, 2019 at 14:36
  • so how can id define empty string in python.Any help on this @CoryKramer Commented Nov 14, 2019 at 4:59

1 Answer 1

1

I would skip both the flag and the explicit for loop, and use any instead.

if any(p[j]['value'] in ('', 'Null') for p in payload for j in attrs):
    print("successfully not processed")
else:
    print("successfully processed")
Sign up to request clarification or add additional context in comments.

4 Comments

why we use any here?what is difference between both of them
any returns true if any of the boolean expressions produced by the generator are true. That's exactly what you are trying to do with your for loop, except you are using an explicit flag to remember when you found a matching condition. any basically sets (and returns) that flag for you.
Thanks @chepner , but for this also it is not sending value instead only shows "successfully not processed"
What value do you want? In your original, y just ends up being the last payload you check, payload[-1]['pressure']['value'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.