Home > database >  How to extract the dictionary if element in list matches
How to extract the dictionary if element in list matches

Time:12-03

I have list of dictionary

  • I need to check first if email_id is exist in the dictionary
  • if exist then return that particular dictionary
  • If email_id doesn't exist then only check the group list
test =  [
    {
      "email": "[email protected]",
      "type": "Group"
    },
    {
      "email": "[email protected]",
      "type": "Single"
    }
  ]

Code is below

group = ['[email protected]', '[email protected]', '[email protected]']


email_id = "[email protected]"


def teat():
    for each in test:
        if email_id == each['email']:
            return (each)
        else:
            for ad_group in group:
                if ad_group == each['email'] :
                    return (each)
teat()  

My output is {'email': '[email protected]', 'type': 'Group'}

but expected is {'email': '[email protected]', 'type': 'Single'}

CodePudding user response:

Because the first condition requires an exhaustive search of the whole list, you really need two loops. One to check the first condition and one for the second.

def teat():
    for each in test:
        if email_id == each['email']:
            return each
    for each in test:
        if each['email'] in group:
            return each

CodePudding user response:

If you don't want to iterate the same list twice, use this method:

def teat():
    email_match = None
    group_match = None
    for each in test:
        if email_id == each['email']:
            email_match = each
            break
        for ad_group in group:
            if ad_group == each['email']:
                group_match = each
    return email_match or group_match

CodePudding user response:

You need to run both for loops separately. In your current code your start searching for a group match as soon as your first email doesn't match.

def teat():
     for each in test:
         if email_id == each['email']:
             return (each)

     for each in test:
         for ad_group in group:
             if ad_group == each['email'] :
                 return (each)

CodePudding user response:

In your code, both if and else are returning something which means it is not iterating through the whole list.

I have modified your code a bit.

def teat():
    for each in test:
        if email_id == each['email']:
            mydict = each
            return mydict
        else:
            for ad_group in group:
                if ad_group == each['email'] :
                    mydict = each
    return mydict

Output:

{'email': '[email protected]', 'type': 'Single'}

CodePudding user response:

Am I correct to say that what you essentially want to return is the last matching dictionary in the list (at least, the preferred answer does seem to do that)? If that is the case, why not processing the list starting from the end and returning the first element which satifies one of the 2 conditions?

def teat():
    for i in range(len(test) - 1, -1, -1):
        each = test[i]
        if email_id == each['email'] or each['email'] in group:
            return each
  • Related