<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Find specific key-value tuples within a dictionary? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258938#M19917</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Are your keys currently row numbers from a table?&amp;nbsp; If so, you are basically treating a dictionary like a list, which is more awkward to work with than if it was just a list.&amp;nbsp; The focus of the dictionary key should be the BuildingID, not the row number.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Assuming you want each building to have the three hazards mentioned above, and only those three hazards, the following (untested) code might work:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# import functions from modules that are available but not commonly imported&amp;nbsp; 
from collections import defaultdict&amp;nbsp; 
from numpy import fromiter, dtype

# group hazards by building
haz_group = defaultdict(list)
with arcpy.da.SearchCursor(in_table, ['BuildingID', 'HazardID']) as cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for k, v in cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; haz_group&lt;K&gt;.append(v)&lt;/K&gt;

# create iterable and populate with flagged buildings
build_iter = (k for (k, v) in haz_group.iteritems() if v.sort() != [16, 17, 18])
tmp1 = fromiter(build_iter, dtype([('BuildingID', 'S10')]))

# dump numpy array to table
arcpy.da.NumPyArrayToTable(tmp1, out_table)

# or just print BuildingIDs to console
for build in build_iter:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print build&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note that if you are going to just print the buildings, drop lines 13 and 16 because line 13 will exhaust the generator so lines 19 and 20 won't print anything.&amp;nbsp; Instead of creating a generator expression, you could use a list comprehension and the list would persist.&amp;nbsp; I tend to work with generator expressions because lists can consume large amounts of memory with very large data sets.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;NOTE:&amp;nbsp; Updated Line 12 to address the HazardID not necessarily being sorted in original table.&lt;/EM&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 12:44:47 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2021-12-11T12:44:47Z</dc:date>
    <item>
      <title>Find specific key-value tuples within a dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258930#M19909</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am trying to build a process that will check a dictionary for an existing key-value combination within a dictionary where the key is a variable and the value is always going to be one of three different values (16, 17, or 18). I quickly came upon the issue of searching a dictionary with a dictionary and found someone's solution to turn the search into a frozenset. That removed the &lt;SPAN style="color: #e23d39;"&gt;TypeError: unhashable type: 'dict'&lt;/SPAN&gt; error message, but the code is still not doing what I want it to do.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am trying to identify that a table with 10's of thousands of Building ID's contains exactly 3 occurrences of each Building ID and that each occurrence is accompanied with only either a 16, 17, or 18 value in the next column. So far I've only been playing around with snippets of code to see if I could make it work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;WIND = 'L_DAMAGE_RESULTS_WIND'
readList = ["BLDG_ID", "HAZARD_ID"]
PassFail = "PASS"
WINDDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(WIND, readList)}
with arcpy.da.SearchCursor(WIND, "BLDG_ID") as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lookup = {row[0]:(18)}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; key = frozenset(lookup.items())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if key not in WINDDict:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PassFail = "fail"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
print PassFail
fail
print lookup
{u'370139999': (18,)}&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I thought the above would result in a "PASS" because u'370139999': (18,) does exist in WINDDict, but it didn't.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm still wrapping my head around dictionaries, so I would appreciate any help that is offered.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;UPDATE:&lt;/P&gt;&lt;P&gt;So the problem appears to be with the unicode within the dictionary.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;WINDDict = {29184: (u'3701310027', 17), 1: (u'370131', 16), 2: (u'3701310', 16), 3: (u'37013100', 16), 4: (u'370131000', 16), 5: (u'3701310000', 16), 6: (u'3701310001', 16), ...}

if (u'3701310027', 17)in WINDDict:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "yes"
else:&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "no"
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
no

if 17 in WINDDict:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "yes"
else:&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "no"
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
yes&lt;/PRE&gt;&lt;P&gt;I have no idea how to handle this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited by: John Lay to add more explanation.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:44:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258930#M19909</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2021-12-11T12:44:39Z</dc:date>
    </item>
    <item>
      <title>Re: Find specific key-value tuples within a dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258931#M19910</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If the data is already in a table, it might be more straightforward to run Summary Statistics.&amp;nbsp; Generating a count, minimum, and maximum of NextColumn against BuildingID should get you what you are after.&amp;nbsp; The count will let you know if there are only three records, and the minimum and maximum will show whether any of the values are less than 16 or more than 18.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Something like:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.Statistics_analysis(in_table,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out_table,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [ ["NextCol", "COUNT"], ["NextCol", "MIN"], ["NextCol", "MAX"] ],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ["BuildIDCol"])&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:44:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258931#M19910</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-11T12:44:41Z</dc:date>
    </item>
    <item>
      <title>Re: Find specific key-value tuples within a dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258932#M19911</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;numpy and pandas are really good at selecting and grouping things.&amp;nbsp; Something like this could work (needs to be tested though)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import numpy
from pandas import *

#create numpy array from the table
nparr = arcpy.da.TableToNumPyArray(WIND, ["BLDG_ID", "HAZARD_ID"])

#turn it into a Pandas data frame
df = DataFrame(nparr, columns=["BLDG_ID", "HAZARD_ID"])

#new data frame with the selection criteria
df2 = df[df['HAZARD_ID'].isin([16,17,18])]

#do something meaningful with the filtered dataframe like convert it to a .csv file
df2.to_csv(r'C:\output.csv')&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:44:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258932#M19911</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-11T12:44:44Z</dc:date>
    </item>
    <item>
      <title>Re: Find specific key-value tuples within a dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258933#M19912</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, but that just makes it too logical and simple &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/wink.png" /&gt;.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Jan 2015 17:10:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258933#M19912</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2015-01-08T17:10:55Z</dc:date>
    </item>
    <item>
      <title>Re: Find specific key-value tuples within a dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258934#M19913</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks James, I haven't had the opportunity to play with numpy yet. I will definitely give this a look. But for the sake of expediency, I'm going to have to go with the &lt;A href="https://community.esri.com/migrated-users/3420"&gt;Joshua Bixby&lt;/A&gt;'s solution.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Jan 2015 17:15:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258934#M19913</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2015-01-08T17:15:20Z</dc:date>
    </item>
    <item>
      <title>Re: Find specific key-value tuples within a dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258935#M19914</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;No problem.&amp;nbsp; I was just working thru grouping issue this morning too and had some success with those libraries.&amp;nbsp; All of that For Loop stuff just can't touch the performance of numpy/pandas --- processing 4.5 million rows in a couple of minutes is no problem.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Jan 2015 17:21:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258935#M19914</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2015-01-08T17:21:11Z</dc:date>
    </item>
    <item>
      <title>Re: Find specific key-value tuples within a dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258936#M19915</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is very easy to do using the .items() dictionary method. Consider:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;dict = {1:16, 2:17, 3:19, ...}&lt;/P&gt;&lt;P&gt;key = 2 #this is your variable&lt;/P&gt;&lt;P&gt;valueList = (16,17,18) # vals you are looking for in the keys. There's only one value per key right?&lt;/P&gt;&lt;P&gt;dictItems = dict.items()&lt;/P&gt;&lt;P&gt;for value in valueList:&lt;/P&gt;&lt;P&gt;&amp;nbsp; if (key, value) in dictItems:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print str((key, value)) + " is in the dictionary!"&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Jan 2015 18:19:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258936#M19915</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2015-01-08T18:19:49Z</dc:date>
    </item>
    <item>
      <title>Re: Find specific key-value tuples within a dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258937#M19916</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Actually, the dictionary would look more like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;dict = {1:(u'37013',16), 2: (u'37013', 17), 3: (u'37013', 18), 4: (u'37014', 16), 5: (u'37014', 17), 6: (u'37014', 18),...} # corresponding with Building ID: Hazard ID.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need to be able to identify that Building 37013 has a row for Hazard ID 16, a row for Hazard 17, and one for Hazard 18. If the table does not meet this criteria, the table fails the check and I need to log the Building ID.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Jan 2015 19:40:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258937#M19916</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2015-01-08T19:40:13Z</dc:date>
    </item>
    <item>
      <title>Re: Find specific key-value tuples within a dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258938#M19917</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Are your keys currently row numbers from a table?&amp;nbsp; If so, you are basically treating a dictionary like a list, which is more awkward to work with than if it was just a list.&amp;nbsp; The focus of the dictionary key should be the BuildingID, not the row number.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Assuming you want each building to have the three hazards mentioned above, and only those three hazards, the following (untested) code might work:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# import functions from modules that are available but not commonly imported&amp;nbsp; 
from collections import defaultdict&amp;nbsp; 
from numpy import fromiter, dtype

# group hazards by building
haz_group = defaultdict(list)
with arcpy.da.SearchCursor(in_table, ['BuildingID', 'HazardID']) as cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for k, v in cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; haz_group&lt;K&gt;.append(v)&lt;/K&gt;

# create iterable and populate with flagged buildings
build_iter = (k for (k, v) in haz_group.iteritems() if v.sort() != [16, 17, 18])
tmp1 = fromiter(build_iter, dtype([('BuildingID', 'S10')]))

# dump numpy array to table
arcpy.da.NumPyArrayToTable(tmp1, out_table)

# or just print BuildingIDs to console
for build in build_iter:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print build&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note that if you are going to just print the buildings, drop lines 13 and 16 because line 13 will exhaust the generator so lines 19 and 20 won't print anything.&amp;nbsp; Instead of creating a generator expression, you could use a list comprehension and the list would persist.&amp;nbsp; I tend to work with generator expressions because lists can consume large amounts of memory with very large data sets.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;NOTE:&amp;nbsp; Updated Line 12 to address the HazardID not necessarily being sorted in original table.&lt;/EM&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:44:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258938#M19917</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-11T12:44:47Z</dc:date>
    </item>
    <item>
      <title>Re: Find specific key-value tuples within a dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258939#M19918</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I agree with Mr. Bixby, no need to store row ids. This code using set() objects also does the job:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;magicNumberSet = set([16,17,18]) #the building must have all of these codes to be in the yesList
buildingDict = {}
searchRows = arcpy.da.SearchCursor('L_DAMAGE_RESULTS_WIND', ["BLDG_ID", "HAZARD_ID"])
for searchRow in searchrows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; buildingId, hazardId = searchRow
&amp;nbsp;&amp;nbsp;&amp;nbsp; if buildingId in buildingDict:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; buildingDict[buildingId].add(hazardId)
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; buildingDict[buildingId] = set([hazardId])
yesList = [buildingId for buildingId in buildingDict if magicNumberSet.issubset(buildingDict[buildingId])]
noList = [set(buildingDict.keys()).difference(yesList)]&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:44:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258939#M19918</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-11T12:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: Find specific key-value tuples within a dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258940#M19919</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I tried building the dictionary with just the Building ID, but the result was a single Building ID key and a single Hazard ID. I probably built it wrong. But like I said originally, I'm still wrapping my head around dictionaries.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;e.g.:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;dict = {1:(u'37013',16), 2: (u'37013', 17), 3: (u'37013', 18), 4: (u'37014', 16), 5: (u'37014', 17), 6: (u'37014', 18),...}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;became&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;dict = {u'37013':18, u'37014':18,...}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;so even if the original code worked it would fail b/c there was no Hazard ID 16 or 17.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;I will give this a look-see a little later this afternoon. I will also give @Chris Snyder' s a look as well.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 09 Jan 2015 11:26:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258940#M19919</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2015-01-09T11:26:18Z</dc:date>
    </item>
    <item>
      <title>Re: Find specific key-value tuples within a dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258941#M19920</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It's not quite that the building must have all three, is is more like there must be 3 buildings with the same ID that each have one of the three hazards. the table would look like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;BLDG_ID&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HAZARD_ID&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OTHER FIELDS&lt;/P&gt;&lt;P&gt;37013&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 16&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; other info unique to HAZ_ID 16&lt;/P&gt;&lt;P&gt;37013&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 17&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; other info unique to HAZ_ID 17&lt;/P&gt;&lt;P&gt;37013&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 18&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; other info unique to HAZ_ID 18&lt;/P&gt;&lt;P&gt;37014&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 16&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; other info unique to HAZ_ID 16&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Like I mentioned to &lt;A href="https://community.esri.com/migrated-users/3420"&gt;Joshua Bixby&lt;/A&gt;‌ above, I will play with this some later this afternoon.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you both for your suggestions.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 09 Jan 2015 11:34:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258941#M19920</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2015-01-09T11:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: Find specific key-value tuples within a dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258942#M19921</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I know there's no interest in pandas, but it really simplifies things and supercharges performance.&amp;nbsp; This along with the arcpy.da.TableToNumpyArrray method it makes it super easy to integrate. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Test data (I just created a .csv file but it could be a gdb table or just about anything else):&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;&lt;EM&gt;BLDG_ID,HAZARD_ID,OTHER FIELDS&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;&lt;EM&gt;37013,11,other info unique to HAZ_ID 11&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;&lt;EM&gt;37013,46,other info unique to HAZ_ID 46&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;&lt;EM&gt;37013,9,other info unique to HAZ_ID 9&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;&lt;EM&gt;37013,16,other info unique to HAZ_ID 16&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;&lt;EM&gt;37013,17,other info unique to HAZ_ID 17&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;&lt;EM&gt;37013,18,other info unique to HAZ_ID 18&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;&lt;EM&gt;37014,8,other info unique to HAZ_ID 8&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;&lt;EM&gt;37014,6,other info unique to HAZ_ID 6&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;&lt;EM&gt;37014,33,other info unique to HAZ_ID 33&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;&lt;EM&gt;37014,16,other info unique to HAZ_ID 16&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;&lt;EM&gt;37014,17,other info unique to HAZ_ID 17&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;&lt;EM&gt;37014,18,other info unique to HAZ_ID 18&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This does exactly what you want OP:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;dat = r"H:\pandas_testdat.csv"
df = pd.read_csv(dat)
df2 = df[df['HAZARD_ID'].isin([16,17,18])]&amp;nbsp; 
print df2.values&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yeah.&amp;nbsp; That's all that is necessary &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Result:&lt;/P&gt;&lt;P&gt;BLDG_ID,HAZARD_ID,OTHER FIELDS&lt;/P&gt;&lt;P&gt;37013L 16L 'other info unique to HAZ_ID 16'&lt;/P&gt;&lt;P&gt;37013L 17L 'other info unique to HAZ_ID 17'&lt;/P&gt;&lt;P&gt;37013L 18L 'other info unique to HAZ_ID 18'&lt;/P&gt;&lt;P&gt;37014L 16L 'other info unique to HAZ_ID 16'&lt;/P&gt;&lt;P&gt;37014L 17L 'other info unique to HAZ_ID 17'&lt;/P&gt;&lt;P&gt;37014L 18L 'other info unique to HAZ_ID 18'&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:44:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258942#M19921</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-11T12:44:52Z</dc:date>
    </item>
    <item>
      <title>Re: Find specific key-value tuples within a dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258943#M19922</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK, This almost does what I was looking for (&lt;A href="https://community.esri.com/migrated-users/3420" target="_blank"&gt;Joshua Bixby&lt;/A&gt;‌ and &lt;A href="https://community.esri.com/migrated-users/7306" target="_blank"&gt;James Crandall&lt;/A&gt;‌ I just haven't gotten to your examples yet. James--had some trouble with installing Panda, but am square now)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm a little lost with it though. Please walk me through the bits I'm missing so that I may apply the info instead of just copy it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;magicNumberSet = set([16,17,18]) 
buildingDict = {}&amp;nbsp; 
searchRows = arcpy.da.SearchCursor('L_DAMAGE_RESULTS_WIND', ["BLDG_ID", "HAZARD_ID"])&amp;nbsp; 
for searchRow in searchrows:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; buildingId, hazardId = searchRow&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; if buildingId in buildingDict:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; buildingDict[buildingId].add(hazardId)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; buildingDict[buildingId] = set([hazardId])&amp;nbsp; 
yesList = [buildingId for buildingId in buildingDict if magicNumberSet.issubset(buildingDict[buildingId])]&amp;nbsp; 
noList = [set(buildingDict.keys()).difference(yesList)]&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chris, I get a little lost around line 9. if &lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;buildingDict[buildingId].add(hazardId) &lt;/SPAN&gt;is appending the value set, I assume is &lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;buildingDict[buildingId] = set([hazardId]) &lt;/SPAN&gt;creating the first instance. I'm not really sure what "=" is supposed to mean here. My brain automatically goes to one is equal to the other which can't be the case.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In line 10, for each key in the dictionary if the value set is a subset of ([16,17,18]) add it to the list. This would mean that sets ([17,18]) and ([17,18,19]) would be excluded from the list, but ([16,17,18,19]) would be added. Using &lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #f6f6f6;"&gt;magicNumberSet.issuperset(buildingDict[buildingId]) &lt;/SPAN&gt;would mean the reverse is true. By replacing it with &lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #f6f6f6;"&gt;magicNumberSet==set(buildingDict[buildingId]) &lt;/SPAN&gt;I would essentially be saying that the sets must be equal before being added to the list. Correct? I need to check that there are always 3 buildings and only 3 buildings with a hazard ID of 16, 17, and 18. If there are only 2 buildings or 4 buildings whatever the hazard value, the table fails. Does the order the values appear in the set matter?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:44:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-specific-key-value-tuples-within-a-dictionary/m-p/258943#M19922</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2021-12-11T12:44:55Z</dc:date>
    </item>
  </channel>
</rss>

