0

I have a parent dictionary

parent = [
  {
    "user": "a@gmail.com",
    "type": "Product Team",
    "message": "Developer",
    "employeeId": 101
  },
  {
    "user": "b@gmail.com",
    "type": "Product Team",
    "message": "Developer",
    "employeeId": "102"
  }
]

My input is

body = {"employeeId":102}

My input will vary for testing

body = {"employeeId":101}

body = {"employeeId":103}

I have search in parent dictionary and retrieve the user employeeId and message if it matches employeeId

  • I need to iterate over the parent dictionary
  • once I found the first match then i have to break out from the loop
  • if not i need to continue the loop
  • if success i need to return a dictionary
  • if employeeId doesnot match in parent dict then it should say employeeId doesnot exist

My code is below

def noid():
    return "No ID"
def new_f():
    for conf in parent :
        if int(conf["employeeId"]) == int(body['employeeId']):
             user = conf['user']
             employeeId= conf["employeeId"]
             message = conf["message"]
             return {'user': user, 'employeeId': employeeId, 'message': message}
             break
        else:
            continue
        return noid()
new_f()

In my code only employeeId:101 is working only for first element is working./ Only first dictionary getting success

  • `solution = list(filter(lambda x: x.get('employeeId',None)==str(body['employeeId']), parent))` – sahasrara62 Jan 26 '21 at 16:39
  • `int(parent ["employeeId"])` => this should be `int(conf["employeeId"])` edit: fixed – Axel Jan 26 '21 at 16:39
  • 1
    can you please clarify your last sentence, what is your problem and the expected behavior ? – Axel Jan 26 '21 at 16:42
  • yes, only first dictinary is getting success –  Jan 26 '21 at 16:56
  • Your code works fine... I ran it with `body = {"employeeId":102}` and printed the user and `b@gmail.com` was printed.... – Tomerikoo Jan 26 '21 at 17:10
  • if employeeId doesnot match in parent dict then it should return employeeId doesnot exist –  Jan 26 '21 at 17:12
  • So add an `else` to the `for`, but this is not clear as to be the problem from your question. You are saying that it only works for `101` but it also works with `102` so please clarify your question – Tomerikoo Jan 26 '21 at 17:16
  • Does this answer your question? [Why does python use 'else' after for and while loops?](https://stackoverflow.com/questions/9979970/why-does-python-use-else-after-for-and-while-loops) – Tomerikoo Jan 26 '21 at 17:18
  • Your code makes no sense. You always return after the first iteration... – Tomerikoo Jan 26 '21 at 18:51
  • @Tomerikoo if success i need to return {dictionary} otherwise return "No ID" –  Jan 27 '21 at 01:52
  • Yes I understood that. But your `return` statement is unconditional in the body's loop which means you never get past the first dictionary. Please see my edit to the answer – Tomerikoo Jan 27 '21 at 08:55
  • added my code which is working –  Jan 27 '21 at 15:04

3 Answers3

1

You're looking for a for/else construct:

for conf in parent:
    if int(conf["employeeId"]) == int(body['employeeId']):
         user = conf['user']
         employeeId = conf["employeeId"]
         message = conf["message"]
         break
else:
    print("No ID found")

According to the edited question where the code is in a function, you can simply return which obviously breaks the loop, so no else is necessary:

def new_f():
    for conf in parent:
        if int(conf["employeeId"]) == int(body['employeeId']):
             user = conf['user']
             employeeId= conf["employeeId"]
             message = conf["message"]
             return {'user': user, 'employeeId': employeeId, 'message': message}

    return "No ID"
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0

Try this:

user, employeeId, message = [''] * 3 # define variables before loop
found = False
for x in parent:
  if int(x['employeeId']) == int(body['employeeId']):
    user = x['user']
    employeeId = x["employeeId"]
    message = x["message"]
    found = True
    break
if found:
  print(user, employeeId, message)
else:
  print("Not found!")
JD12
  • 94
  • 6
  • 1
    How is that different from the original code? Also you never used `x`. It should probably be `for conf in parent:` – Tomerikoo Jan 26 '21 at 17:11
  • if employeeId doesnot match in parent dict then it should return employeeId doesnot exist –  Jan 26 '21 at 17:13
  • You can actually remove the `if found` block. The `else` can be directly under the `for` – Tomerikoo Jan 26 '21 at 17:17
0
def noid():
    return "No ID"
def new_f():
    flag = 0
    for conf in parent :
        if int(conf["employeeId"]) == int(body['employeeId']):
             flag = 1
             user = conf['user']
             employeeId= conf["employeeId"]
             message = conf["message"]
             return {'user': user, 'employeeId': employeeId, 'message': message}
             break
        else:
            return noid()
if flag = 0:
        return noid()