0
[{'reg number': 19150001.0, 'Email ID': 'aaduld2001@gmail.com', 'Department': 'CHE'}, {'reg number': 19150002.0, 'Email ID': 'uabhi165@gmail.com', 'Department': 'CHE'}, {'reg number': 19150003.0, 'Email ID': 'abhirav2001@gmail.com', 'Department': 'CHE'}, {'reg number': 19150966.0, 'Email ID': 'afsathafsal786@gmail.com', 'Department': 'CHE'}, {'reg number': 19150004.0, 'Email ID': 'aiswaryasankar345@gmail.com', 'Department': 'CHE'}, {'reg number': 19150967.0, 'Email ID': 'ajmalaj1334@gmail.com', 'Department': 'CHE'}]

from this dictionary i want to create a list of canidates those who have Department == 'che' or 'CHE' and another list of canidates those who have Department == 'ci' or 'CI'

for canidate in canidates:
if(canidate ['Department']==('che'or'CHE')):
    che.append(canidate['reg number'])
elif(canidate['Department']=='cl'or'CI'):
    ci.append(canidate['reg number'])
scotty3785
  • 6,763
  • 1
  • 25
  • 35
  • 1
    Does this answer your question? [Why does "a == x or y or z" always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true) – Gino Mempin Feb 23 '22 at 09:15
  • The variables `che` and `ci` are not defined. Can you please resubmit the code in a way that users can replicate the problem. – D.L Feb 23 '22 at 10:15

1 Answers1

0

You can use a list comprehension for this, for example

data = [{'reg number': 19150001.0, 'Email ID': 'aaduld2001@gmail.com', 'Department': 'CHE'}, {'reg number': 19150002.0, 'Email ID': 'uabhi165@gmail.com', 'Department': 'CHE'}, {'reg number': 19150003.0, 'Email ID': 'abhirav2001@gmail.com', 'Department': 'CHE'}, {'reg number': 19150966.0, 'Email ID': 'afsathafsal786@gmail.com', 'Department': 'CHE'}, {'reg number': 19150004.0, 'Email ID': 'aiswaryasankar345@gmail.com', 'Department': 'CHE'}, {'reg number': 19150967.0, 'Email ID': 'ajmalaj1334@gmail.com', 'Department': 'CHE'}]

che_candidates = [candidate for candidate in data if candidate['Department'].upper() == 'CHE']

this will get the che and CHE candidates. Use a similar one for the other department. Alternatively, you can use in to compare multiple values at once.

che_candidates = [candidate for candidate in data if candidate['Department'] in ['CHE','che']]
scotty3785
  • 6,763
  • 1
  • 25
  • 35
  • TypeError: string indices must be integers – madhu sankar Feb 23 '22 at 09:39
  • @madhusankar I'm afraid that the error message doesn't mean anything to me without some context. Is this with the code I shared? Or something you have created from what I shared. The example I shared above, does not give that error. – scotty3785 Feb 23 '22 at 09:47