Reputation: 17
This question might be very silly but i am trying to use the dictionary to iterate and return the result. I know how to iterate over dictionary but i want to check if the entered key exists in the dictionary and i want the program to print if the value is present or not.
class companyx:
def __init__(self,empid):
self.empid=empid
def employees(self):
employees={1:'Jane',2:'David',3:'Chris',4:'Roger'}
entered=self.empid
for emp in employees :
if emp == entered:
print ('Hi '+employees[emp] +' you are an employee of companyx.com')
print('You dont belong here')
emp=companyx(2)
emp.employees()
When i pass a parameter which is not in the dictionary, i want the function to print "You dont belong here"
Upvotes: 1
Views: 20264
Reputation: 366133
The most pythonic way to do this is to just try the lookup, and handle failure if it happens:
try:
print('Hi '+employees[entered] +' you are an employee of companyx.com')
except KeyError:
print('You dont belong here')
There's no reason for the for
loop; the whole point of dictionaries is that you can look things up in one step, d[key]
, instead of having to loop over the keys and check whether each one == key
.
You could use in
to check whether the key exists, and then look it up. But that's a little silly—you're looking up the key to see whether you can look up the key. Why not just look up the key?
You can do this by using the get
method, which returns None
(or you can pass a different default value) if the key is missing:
name = employees.get(entered)
if name:
print('Hi '+name +' you are an employee of companyx.com')
else:
print('You dont belong here')
But it's Easier to Ask Forgiveness than Permission. Besides being slightly more concise, using try
and except
makes it clear that finding the name is the normal case that should be true, and not finding it is the exceptional case.
Upvotes: 2
Reputation: 236150
The easiest (and most idiomatic) way to check if a key is in a dictionary is:
if entered in employees:
The above replaces the for/if
part of your code. Notice that there's no need to explicitly traverse the dictionary, the in
operator takes care of checking membership. Short and sweet :) The complete code would look like this:
def employees(self):
employees = {1:'Jane', 2:'David', 3:'Chris', 4:'Roger'}
if self.empid in employees:
print('Hi ' + employees[self.empid] + ' you are an employee of companyx.com')
else:
print("You don't belong here")
Upvotes: 9
Reputation:
Use the in
keyword to quickly perform dictionary lookups:
if entered in employees:
# the key is in the dict
else:
# the key could not be found
Upvotes: 3
Reputation: 20992
You don't need to iterate through the dictionary to do that. You can write:
def employees(self):
employees={1:'Jane',2:'David',3:'Chris',4:'Roger'}
employee = employees.get(self.empid)
if employee:
print ('Hi ' + employee + ' you are an employee of companyx.com')
else:
print ('You dont belong here')
Upvotes: 2
Reputation: 4006
No need for a for loop - you just need:
if entered in employees:
print 'blah'
else:
print 'You do not belong here'
Upvotes: 1