Skip to content
Advertisement

Identifying empty list in JSON data in Python

I have a json data set I’m trying to insert into a database. To do so, I need placeholders “” for non-existent data. In the example below, if there’s no email address, I need the insert statement to be (….,”boo”,….) instead of (….,”me@mail.com”,….). I have tried checking for list length, using not, email == [], etc. Every way I know how or have Googled, and none are working.

Example data:

JavaScript

My code for this particular section:

JavaScript

The if statement (regardless of how I’ve tried so far), does not execute, and I get a printed list of only the email addresses if they exist:

JavaScript

I need:

JavaScript

Advertisement

Answer

If the list of "emailAddresses" is empty, the for loop doesn’t get executed. Accordingly print("boo") neither.

You need to check the list first, before iterate through the list:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement