2

i need to compare the two JSON arrays below, and end up with the below emails_new result ... The stick in the mud I'm finding is one of the arrays has an extra key/value pair, which I need in the updated array. So struggling to match.

Essentially; 'if any email(s) in emails_exist appear in emails_all, don't include them as part of emails_new'

Existing Arrays

emails_all = [{'email': '[email protected]', 'first_name': 'New Name'}, {'email': '[email protected]', 'first_name': 'Exists Name'}]

emails_exist = [{'email': '[email protected]'}]

Removing all of the emails_exist values from the emails_all, I only want to build a list to show the email/first_name of the emails that do not appear in emails_exist ... Similar to the below emails_new

Output

emails_new = [{'email': '[email protected]', 'first_name': 'New'}]

Possibly worthy of note: these JSON arrays may have up to 100 items in each.

1
  • as far as I can tell, your question has nothing to do with JSON Commented May 27, 2018 at 8:48

2 Answers 2

2

Just use a simple helper function

def contains(emails, email):                      
    for contained_email in emails:                   
        if email["email"] == contained_email["email"]: 
            return True                                 
    return False                                    

emails_new = [email for email in emails_all if not contains(emails_exist, email)]
Sign up to request clarification or add additional context in comments.

3 Comments

Very clever @peter , and for my lack of sleep brain, this 'simple helper' function is liquid Adderall, thanks. One thing, the resulting array still need to have the first_name key/value ... Currently it spits out the [{'email': '[email protected]'},] only. Possible?
@Danny Are you sure? The new array contains only entries from emails_all wihtout modifying them in any way. They should still contain the first_name. It just filters out the entries that return True using the contains function.
Ugh, apologies, I'm on no sleep ... I'd removed the ProjectionExpression from the DDB call while testing, what a 15 years in the business, amateur. Thanks Peter, this works perfectly. I do not
0

@Danny, you can also get the expected output with just 1 line of code using Python's list comprehension, map(), filter(), lambda function etc. as the below code examples shows.

Please comment if you face any problem while reading & understanding my code.

Check Lambda, filter(), map() and List comprehension. It's really good to use these concepts in programming.

1 line code

emails_new = list(filter(lambda email_all_dict: True if sum(list(map(lambda email_exist_dict: True if email_exist_dict["email"] == email_all_dict['email'] else False, emails_exist))) == 0 else False, emails_all))

Now have a look at the below code examples one by one.

Example 1

emails_all = [{'email': '[email protected]', 'first_name': 'New Name'}, {'email': '[email protected]', 'first_name': 'Exists Name'}]

emails_exist = [{'email': '[email protected]'}]

emails_new = list(filter(lambda email_all_dict: True if sum(list(map(lambda email_exist_dict: True if email_exist_dict["email"] == email_all_dict['email'] else False, emails_exist))) == 0 else False, emails_all))

print(emails_new)

# [{'first_name': 'New Name', 'email': '[email protected]'}]

Example 2

emails_all = [
        {'email': '[email protected]', 'first_name': 'New Name1'}, 
        {'email': '[email protected]', 'first_name': 'Exists Name1'},
        {'email': '[email protected]', 'first_name': 'Exists Name2'},
        {'email': '[email protected]', 'first_name': 'Exists Name5'},
        {'email': '[email protected]', 'first_name': 'Exists Name6'},
        {'email': '[email protected]', 'first_name': 'New Name9'}, 
        {'email': '[email protected]', 'first_name': 'Exists Name7'},
        {'email': '[email protected]', 'first_name': 'Exists Name8'},
        {'email': '[email protected]', 'first_name': 'Exists Name4'},
        {'email': '[email protected]', 'first_name': 'New Name4'}, 
        {'email': '[email protected]', 'first_name': 'New Name2'}, 
        {'email': '[email protected]', 'first_name': 'Exists Name3'}
]

emails_exist = [
        {'email': '[email protected]'},    # Does not exist
        {'email': '[email protected]', 'first_name': 'Exists Name7'},
        {'email': '[email protected]', 'first_name': 'Exists Name8'},
        {'email': '[email protected]', 'first_name': 'Exists Name4'}
]

emails_new2 = list(filter(lambda email_all_dict: True if sum(list(map(lambda email_exist_dict: True if email_exist_dict["email"] == email_all_dict['email'] else False, emails_exist))) == 0 else False, emails_all))

# Pretty printing list
print(json.dumps(emails_new2, indent=4))

"""
[
    {
        "first_name": "New Name1",
        "email": "[email protected]"
    },
    {
        "first_name": "Exists Name1",
        "email": "[email protected]"
    },
    {
        "first_name": "Exists Name2",
        "email": "[email protected]"
    },
    {
        "first_name": "Exists Name5",
        "email": "[email protected]"
    },
    {
        "first_name": "Exists Name6",
        "email": "[email protected]"
    },
    {
        "first_name": "New Name9",
        "email": "[email protected]"
    },
    {
        "first_name": "New Name4",
        "email": "[email protected]"
    },
    {
        "first_name": "New Name2",
        "email": "[email protected]"
    },
    {
        "first_name": "Exists Name3",
        "email": "[email protected]"
    }
]
"""

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.