0

I keep getting IndexError: list index out of range but I know where the problem is in the json I am mapping.

server_names_and_ips = [
            (x['virtualMachine']['name'], x['virtualMachine']['network']['publicIpAddresses'][0]['ipAddress']) for x in
            s if x['virtualMachine']['network']['publicIpAddresses'][0]['ipAddress'] is not None]

The problem is x['virtualMachine']['network']['publicIpAddresses'][0]['ipAddress'] has a value you [] in one of the items returned. I tried to put the if condition in for it but that didnt work correctly. Is something wrong with how I wrote the if condition? The desired outcome who be...if it has [] dont add that item to list and move onto the next.

1
  • Share a sample json. As it is, the ask isn't clear. Commented Jul 28, 2019 at 22:35

1 Answer 1

3

Try checking if the list has any elements:

server_names_and_ips = [
        (x['virtualMachine']['name'], x['virtualMachine']['network']['publicIpAddresses'][0]['ipAddress']) for x in
        s if len(x['virtualMachine']['network']['publicIpAddresses']) > 0 and x['virtualMachine']['network']['publicIpAddresses'][0]['ipAddress'] is not None]
Sign up to request clarification or add additional context in comments.

Comments

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.