11
    dict1=({"EMP$$1":1,"EMP$$2":2,"EMP$$3":3})

How to check if EMP exists in the dictionary using python

   dict1.get("EMP##") ??
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Rajeev
  • 44,985
  • 76
  • 186
  • 285
  • It is not entirely clear what you are trying to do. Can you rephrase your question? Providing sample inputs and output would help. – Manoj Govindan Sep 17 '10 at 13:56

5 Answers5

31

It's not entirely clear what you want to do.

You can loop through the keys in the dict selecting keys using the startswith() method:

>>> for key in dict1:
...     if key.startswith("EMP$$"):
...         print "Found",key
...
Found EMP$$1
Found EMP$$2
Found EMP$$3

You can use a list comprehension to get all the values that match:

>>> [value for key,value in dict1.items() if key.startswith("EMP$$")]
[1, 2, 3]

If you just want to know if a key matches you could use the any() function:

>>> any(key.startswith("EMP$$") for key in dict1)
True
David Webb
  • 190,537
  • 57
  • 313
  • 299
  • I had tried this as in your first example and i put an else after it,the if part condition seems to be wrong.... – Rajeev Sep 17 '10 at 14:18
  • 1
    I tried this approach and it works as the author intended. Adding an else works for me as well. What error are you getting? – Tim W. Sep 17 '10 at 14:35
8

This approach strikes me as contrary to the intent of a dictionary.

A dictionary is made up of hash keys which have had values associated with them. The benefit of this structure is that it provides very fast lookups (on the order of O(1)). By searching through the keys, you're negating that benefit.

I would suggest reorganizing your dictionary.

dict1 = {"EMP$$": {"1": 1, "2": 2, "3": 3} }

Then, finding "EMP$$" is as simple as

if "EMP$$" in dict1:
    #etc...
Tim W.
  • 834
  • 1
  • 9
  • 18
2

You need to be a lot more specific with what you want to do. However, assuming the dictionary you gave:

 dict1={"EMP$$1":1, "EMP$$2":2, "EMP$$3":3}

If you wanted to know if a specific key was present before trying to request it you could:

dict1.has_key('EMP$$1') 
True

Returns True as dict1 has the a key EMP$$1.

You could also forget about checking for keys and rely on the default return value of dict1.get():

dict1.get('EMP$$5',0)
0

Returns 0 as default given dict1 doesn't have a key EMP$$5.

In a similar way you could also use a `try/except/ structure to catch and handle missed keys:

try:
    dict1['EMP$$5']
except KeyError, e:
    # Code to deal w key error
    print 'Trapped key error in dict1 looking for %s' % e

The other answers to this question are also great, but we need more info to be more precise.

dtlussier
  • 3,018
  • 2
  • 26
  • 22
  • SilentGhost is correct. Use the form 'key in dict' rather than dict.has_key(key). http://docs.python.org/library/stdtypes.html#dict.has_key – Tim W. Sep 17 '10 at 14:49
1

There's no way to match dictionary keys like this. I suggest you rethink your data structure for this problem. If this has to be extra quick you could use something like a suffix tree.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
0

You can use in string operator that checks if item is in another string. dict1 iterator returns list of keys, so you check "EMP$$" against of each dict1.key.

dict1 = {"EMP$$1": 1, "EMP$$2": 2, "EMP$$3": 3}

print(any("EMP$$" in i for i in dict1))
# True

# testing for item that doesn't exist
print(any("AMP$$" in i for i in dict1))
# False
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179