how to find a match in a dictionary

python dictionary match key values
python dictionary partial key match
python find string in dictionary value
python match string to dictionary key
python find in dictionary
python find dictionary in list by value
how to compare string with dictionary in python
find keys of dictionary python

I have two dictionaries, RFQDict and AwardsDict. I want to take the keys of RFQdict and search through AwardsDict values for matches.

So I tried this

RFQDict = {}
AwardsDict = {}

# Fetch RFQ 

RFQref = db.reference('TestRFQ')

snapshot = RFQref.get()
for key, val in snapshot.items():
    RFQDict[key] = val
    print('{0} => {1}'.format(key, val))


Awardsref = db.reference('DibbsAwards')

dsnapshot = Awardsref.get()
for key, val in dsnapshot.items():
    AwardsDict[key] = val
    print('{0} => {1}'.format(key, val))



for key in RFQDict:
    if key in AwardsDict.values():
        print(key+ " Match found")

is this the way to do it or there is a better way and how could return the key and values where the match was found?

In python3 you can do AwardsDict.values() & RFQDict.keys() and you will get a set with the common key/values.

The '&' operator is used for set intersection and works with the dictionary views returned by values() and keys(). More information of the view returned by those methods: https://docs.python.org/3/library/stdtypes.html?highlight=dictview#dictionary-view-objects

Python, [value for key, value in programs.items() if 'new york' in key.lower()]. For better performance, you should iterate over the list and check for membership in the dictionary: for k in my_list: if k in my_dict: print k, my_dict[k] If you want to create a new dictionary from these key-value pairs, use. new_dict = {k: my_dict[k] for k in my_list if k in my_dict}

If you want to store the keys and values that match, it would probably be best to store the key and value from the second dictionary since if you just store the matching key and value you will have elements like (a, a) which won't really tell you much about where they matched in the second dictionary. Maybe something like this

d1 = {'a': 1, 'b': 2, 'c': 3}
d2 = {'x': 'a', 'y': 1, 'z': 'c'}

res = [(i, {j: d2[j]}) for i in d1 for j in d2 if i == d2[j]]
print(res)
# [('a': {'x': 'a'}), ('c': {'z': 'c'})]

Regular Expressions in Python | Set 2 (Search, Match and Find All) · manjeet_04. Check out this Author's contributed articles. If you like GeeksforGeeks and would​  Definition of match (Entry 2 of 3) transitive verb. 1 a : to encounter successfully as an antagonist. b (1) : to set in competition or opposition. (2) : to provide with a worthy competitor. c : to set in comparison. 2 : to join or give in marriage. 3 a (1) : to put in a set possessing equal or harmonizing attributes.

I would do a list comprehension:

result=[x for x in AwardsDict.values() if x in RFQDict.keys() ]

This way you get a list keeping the duplicates. That is, if a RFQ key is presented in more than one value in AwardsDict. With the & operator you loss that information (as sets only have unique elements).

For example:

RFQDict = {}
AwardsDict = {}

for i in range(5):
    RFQDict[i]=0

for i in range(5):
    AwardsDict[i]=i

for i in range(5,11):
    AwardsDict[i]=i//2 #integer division, i=8 and i=9 get a value of 4 


result=[x for x in AwardsDict.values() if x in RFQDict.keys() ]
print('{}'.format(result))

#output [0, 1, 2, 3, 4, 2, 3, 3, 4, 4]

For this, we just use naive check and compare and return the result once we find the suitable match and break for rest of dictionaries. filter_none. edit close. Python | Substring Key match in dictionary Sometimes, while working with dictionaries, we might have a use case in which we are not known the exact keys we require but just a specific part of keys that we require to fetch.

I am trying to match python dictionary value. Here I store value in string type. When any new string is coming its check weather already string available in  Find keys by value in dictionary. As, dict.items() returns an iterable sequence of all key value pairs in dictionary. So, we will iterate over this sequence and for each entry we will check if value is same as given value then we will add the key in a separate list i.e.

items() and for each entry we will check if its value matches with any entry from the given value list, if yes then we will add that key in a separate  How to find a key in a Dictionary with C#. The ContainsKey method checks if a key already exists in the dictionary. The ContainsKey method checks if a key already exists in the dictionary. The following code snippet checks if a key already exits and if not, adds it. The ContainsValue method checks if a value already exists in the dictionary.

print(item). This will print all the key that matches a particular values in the dictionary. Hope it enough to get a clarity regarding your answer  thisTag = _tags.FirstOrDefault(t => t.Key == tag); is an inefficient and a little bit strange way to find something by key in a dictionary. Looking things up for a Key is the basic function of a Dictionary.

Comments
  • @e.s. can you please post as an answer with explanation
  • how do we check if their is no match?