<?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: How to populate a list using multiple fields from a table? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-populate-a-list-using-multiple-fields-from/m-p/66858#M5484</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Christi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Are you familiar with dictionaries?&amp;nbsp; This would be a great way to generate a list for each variable that could be called by using the parcel ID.&amp;nbsp; I included an example of how to create a dictionary with all 3 attributes you wanted.&amp;nbsp; These attributes can be accessed by the unique parcel ID which would serve as the key for the dictionary.&amp;nbsp; The example I showed at the bottom demonstrates how to print &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"Parcel ID:&amp;nbsp;&amp;nbsp;&amp;nbsp; address, city"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For example, it would show something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;0360-15-16-200-002-0:&amp;nbsp;&amp;nbsp;&amp;nbsp; 101 Main St, Springfield&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The advantage of using a dictionary like this is that you can keep all these items grouped together by parcel ID.&amp;nbsp; If you wanted to add these fields to another table, you could do so easily if the second table has the common Parcel ID field without even needing to join the tables.&amp;nbsp; You could simply use an update cursor with this dictionary.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
#import modules and define workspace
import arcpy
import os
import traceback


workspace = arcpy.env.workspace = "E:/dev/Christi/PSL/" 

arcpy.env.overwriteOutput = True

#Define map document
mxDoc = arcpy.mapping.MapDocument("CURRENT")


#List the first dataframe (named Layers) in the map document
df = arcpy.mapping.ListDataFrames(mxDoc, "Layers") [0]


#List first map layer (which is the parcels layer) in dataframe
parcelLayer = arcpy.mapping.ListLayers(mxDoc,"",df) [0]


#Select parcel layer by attribute and clear any previously selected features
#Set up a Search Cursor to go thru the attribute table and get row values 
#Set up variable, then read the row values for 'APN_NU_1' field and populate the list

arcpy.SelectLayerByAttribute_management(parcelLayer,"CLEAR_SELECTION")

info_dict = {}
rows = arcpy.SearchCursor(parcelLayer)
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; info_dict[row.Parcel_ID] = [row.APN_NU_1,row.ADDR_B,row.CITY]
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
del row,rows

# If you want to access a single field you can reference it with a list index
# example:&amp;nbsp; if you want the address B for a certain parcel you can print something
# like this:&amp;nbsp; print info_dict[row.Parcel_ID][1]
# the [1] indexes item 1 (value) in your list for each dictionary key (parcel ID)
# So to print each address, city for each Parcel ID:

for key,value in info_dict_iteritems():
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '%s:\t%s, %s' %(key,value[1],value[2])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 22:34:13 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2021-12-10T22:34:13Z</dc:date>
    <item>
      <title>How to populate a list using multiple fields from a table?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-populate-a-list-using-multiple-fields-from/m-p/66857#M5483</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm using ArcGIS 10.0 (ArcInfo level). Below is a script that creates an empty list, loops thru a parcels feature class, gets the value for each record in the 'APN_NU_1' field and populates the list with the values.&amp;nbsp; This runs perfectly.&amp;nbsp; How do I write this script to populate the list with multiple fields and their values, say 'APN_NU_1', 'ADDR_B', and 'CITY'?&amp;nbsp; I will then be using this list to call for further programming.. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import modules and define workspace
import arcpy
import os
import traceback


workspace = arcpy.env.workspace = "E:/dev/Christi/PSL/" 

arcpy.env.overwriteOutput = True

#Define map document
mxDoc = arcpy.mapping.MapDocument("CURRENT")


#List the first dataframe (named Layers) in the map document
df = arcpy.mapping.ListDataFrames(mxDoc, "Layers") [0]


#List first map layer (which is the parcels layer) in dataframe
parcelLayer = arcpy.mapping.ListLayers(mxDoc,"",df) [0]


#Select parcel layer by attribute and clear any previously selected features
#Set up a Search Cursor to go thru the attribute table and get row values 
#Set up variable, then read the row values for 'APN_NU_1' field and populate the list

arcpy.SelectLayerByAttribute_management(parcelLayer,"CLEAR_SELECTION")

rows = arcpy.SearchCursor(parcelLayer)
stringList = []
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stringList.append(row.getValue('APN_NU_1'))&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for any immediate feedback,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Christi&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 23 Feb 2013 16:14:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-populate-a-list-using-multiple-fields-from/m-p/66857#M5483</guid>
      <dc:creator>ChristiNelson1</dc:creator>
      <dc:date>2013-02-23T16:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to populate a list using multiple fields from a table?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-populate-a-list-using-multiple-fields-from/m-p/66858#M5484</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Christi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Are you familiar with dictionaries?&amp;nbsp; This would be a great way to generate a list for each variable that could be called by using the parcel ID.&amp;nbsp; I included an example of how to create a dictionary with all 3 attributes you wanted.&amp;nbsp; These attributes can be accessed by the unique parcel ID which would serve as the key for the dictionary.&amp;nbsp; The example I showed at the bottom demonstrates how to print &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"Parcel ID:&amp;nbsp;&amp;nbsp;&amp;nbsp; address, city"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For example, it would show something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;0360-15-16-200-002-0:&amp;nbsp;&amp;nbsp;&amp;nbsp; 101 Main St, Springfield&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The advantage of using a dictionary like this is that you can keep all these items grouped together by parcel ID.&amp;nbsp; If you wanted to add these fields to another table, you could do so easily if the second table has the common Parcel ID field without even needing to join the tables.&amp;nbsp; You could simply use an update cursor with this dictionary.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
#import modules and define workspace
import arcpy
import os
import traceback


workspace = arcpy.env.workspace = "E:/dev/Christi/PSL/" 

arcpy.env.overwriteOutput = True

#Define map document
mxDoc = arcpy.mapping.MapDocument("CURRENT")


#List the first dataframe (named Layers) in the map document
df = arcpy.mapping.ListDataFrames(mxDoc, "Layers") [0]


#List first map layer (which is the parcels layer) in dataframe
parcelLayer = arcpy.mapping.ListLayers(mxDoc,"",df) [0]


#Select parcel layer by attribute and clear any previously selected features
#Set up a Search Cursor to go thru the attribute table and get row values 
#Set up variable, then read the row values for 'APN_NU_1' field and populate the list

arcpy.SelectLayerByAttribute_management(parcelLayer,"CLEAR_SELECTION")

info_dict = {}
rows = arcpy.SearchCursor(parcelLayer)
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; info_dict[row.Parcel_ID] = [row.APN_NU_1,row.ADDR_B,row.CITY]
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
del row,rows

# If you want to access a single field you can reference it with a list index
# example:&amp;nbsp; if you want the address B for a certain parcel you can print something
# like this:&amp;nbsp; print info_dict[row.Parcel_ID][1]
# the [1] indexes item 1 (value) in your list for each dictionary key (parcel ID)
# So to print each address, city for each Parcel ID:

for key,value in info_dict_iteritems():
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '%s:\t%s, %s' %(key,value[1],value[2])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:34:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-populate-a-list-using-multiple-fields-from/m-p/66858#M5484</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-10T22:34:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to populate a list using multiple fields from a table?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-populate-a-list-using-multiple-fields-from/m-p/66859#M5485</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks so much Caleb - this is exactly the direction I thought I needed to go in, but just didn't know how.&amp;nbsp; Question for you:&amp;nbsp; what is info_dict_iteritems() equal to in this case?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Feb 2013 18:27:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-populate-a-list-using-multiple-fields-from/m-p/66859#M5485</guid>
      <dc:creator>ChristiNelson1</dc:creator>
      <dc:date>2013-02-25T18:27:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to populate a list using multiple fields from a table?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-populate-a-list-using-multiple-fields-from/m-p/66860#M5486</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;He meant &lt;/SPAN&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;info_dict.iteritems()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Feb 2013 18:40:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-populate-a-list-using-multiple-fields-from/m-p/66860#M5486</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2013-02-25T18:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to populate a list using multiple fields from a table?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-populate-a-list-using-multiple-fields-from/m-p/66861#M5487</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;He meant &lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;info_dict.iteritems()&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Yes, that was supposed to be a period not underscore, thanks Mathew!&amp;nbsp; I just used the iteritems() method to demonstrate how to iterate through a dictionary and in that case to pull keys and values separately and print them.&amp;nbsp; You mentioned you wanted to use these values later in your script, what are you going to do with them?&amp;nbsp; Using a dictionary will allow you to have the list of those 3 attributes tied to each Parcel ID (or whatever unique identifier you want to use as the key for the dict) and you can access a specific one by indexing the list.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Feb 2013 19:59:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-populate-a-list-using-multiple-fields-from/m-p/66861#M5487</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-02-25T19:59:33Z</dc:date>
    </item>
  </channel>
</rss>

