Iterating through a dictionary

504
7
Jump to solution
05-06-2019 02:33 PM
JoeBorgione
MVP Esteemed Contributor

I have a dictionary in the form of:  

year2018 = {'jan18':(1,2018),'feb18':(2,2018),'mar18':(3,2018), 'apr18':(4,2018),
            'may18':(5,2018),'jun18':(6,2018),'jul18':(7,2018),'aug18':(8,2018),
            'sept18':(9,2018),'oct18':(10,2018),'nov18':(11,2018),'dec18':(12,2018)
            }

I have a data table where each record has Month field and a Year field.  I would like to iterate through my dictionary so that while key = jan18, I select records from my table where Month = 1 and Year = 2018, and then export those records to a stand-alone table called jan18: then move on to key = feb18, etc etc.  I've tried a couple different approaches, but still not quite getting what I want. Any suggestions are appreciated. (Using a dictionary is cool, but I'm open to all suggestions...)

That should just about do it....
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

you ruled out Split By Attributes—Help | ArcGIS Desktop ?

might require adding a temporary field to concatenate your month and year if your data aren't set up for a straight split as is

View solution in original post

7 Replies
hashimotoy2
New Contributor III

Hi Joe, I'm not sure what you've attempted but dictionaries are indeed cool. I haven't tested this but give this a whirl:

for key, value in year2018.items(): # Iterate through each key,value pair in the dictionary
    in_features = in_feature_class # Identify the input feature class
    out_feature_class = out_path + "_" + key # Identify the filepath output appending the key to identify it
    where_clause = "Month = '" + str(value[0]) + "' AND Year = '" + str(value[1]) + "'"
    arcpy.Select_analysis(in_features, out_feature_class, where_clause)
JoeBorgione
MVP Esteemed Contributor

Thanks-  I'm working on a table, not a feature class so I don't think Select_Analysis() will work.  However, If I make a table view first, and run my selections on it, this should give me what I'm after....

That should just about do it....
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

you ruled out Split By Attributes—Help | ArcGIS Desktop ?

might require adding a temporary field to concatenate your month and year if your data aren't set up for a straight split as is

JoeBorgione
MVP Esteemed Contributor

I've never used this tool before: it's my new favorite.  It's fun to write python and do all sorts of elaborate gyrations, but hey, this is slick!

That should just about do it....
DanPatterson_Retired
MVP Esteemed Contributor

Joe... 15 years ago or so...

Split Layer By Attribute 

When it wasn't free.  Mine still has the better Thumbnail

JoshuaBixby
MVP Esteemed Contributor
RandyBurton
MVP Regular Contributor

You can iterate through a dictionary, but some simple loops are easy.  Just ideas..

months = {1:'jan',2:'feb',3:'mar',4:'apr',5:'may',6:'jun',7:'jul',8:'aug',9:'sep',10:'oct',11:'nov',12:'dec'}
for yr in range(2018,2019): # stop when 2019 is reached
    for mo in months:
        print yr, mo, months[mo]
        print "Month = {} AND Year = {}".format(mo,yr)
        print "{}_{}".format(months[mo], yr)

''' prints:
2018 1 jan
Month = 1 AND Year = 2018
jan_2018
... etc.
'''
for yr in range(2018, 2019): # stop when 2019 is reached
    for mo in range(1,13): # stop when 2019 is reached
        print yr, mo

''' prints
2018 1
2018 2
...
2018 11
2018 12
'''