<?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: Check for existence of a field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533257#M41773</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Your code will work with no problems.&amp;nbsp; You can also use the 'arcpy.ListFields' function.&amp;nbsp; Ex:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;lstFields = arcpy.ListFields(fc)

x = False

for field in lstFields:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if field.name == "USNG":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Field exists"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = True

if x &amp;lt;&amp;gt; True:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Field does not exist"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tested the Describe function vs the ListFields function using the time module, and the ListFields function was slightly faster by a tenth of a second.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, to preserve indentation when copying/pasting your code, select your code and click the '#' button at the top.&amp;nbsp; It will wrap it in CODE tags.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 23:11:17 GMT</pubDate>
    <dc:creator>JakeSkinner</dc:creator>
    <dc:date>2021-12-11T23:11:17Z</dc:date>
    <item>
      <title>Check for existence of a field</title>
      <link>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533255#M41771</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;How do you check for the existence of a field in a feature class?&amp;nbsp; v10.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I did this, but it seems clunky.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;fc = r"C:\temp\test.shp"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;desc = arcpy.Describe(fc)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;flds = desc.fields&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;fldin = 'no'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for fld in flds:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; if fld.name == 'USNG':&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fldin = 'yes'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;print fldin&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Aug 2011 15:07:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533255#M41771</guid>
      <dc:creator>MikeDriver</dc:creator>
      <dc:date>2011-08-03T15:07:35Z</dc:date>
    </item>
    <item>
      <title>Re: Check for existence of a field</title>
      <link>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533256#M41772</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Lines:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if fld.name == 'USNG':&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;fldin = 'yes'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;should be indented, of course.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Aug 2011 15:09:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533256#M41772</guid>
      <dc:creator>MikeDriver</dc:creator>
      <dc:date>2011-08-03T15:09:01Z</dc:date>
    </item>
    <item>
      <title>Re: Check for existence of a field</title>
      <link>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533257#M41773</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Your code will work with no problems.&amp;nbsp; You can also use the 'arcpy.ListFields' function.&amp;nbsp; Ex:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;lstFields = arcpy.ListFields(fc)

x = False

for field in lstFields:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if field.name == "USNG":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Field exists"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = True

if x &amp;lt;&amp;gt; True:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Field does not exist"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tested the Describe function vs the ListFields function using the time module, and the ListFields function was slightly faster by a tenth of a second.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, to preserve indentation when copying/pasting your code, select your code and click the '#' button at the top.&amp;nbsp; It will wrap it in CODE tags.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:11:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533257#M41773</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T23:11:17Z</dc:date>
    </item>
    <item>
      <title>Re: Check for existence of a field</title>
      <link>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533258#M41774</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;lstFields = arcpy.ListFields(fc)

if "USNG" in lstFields:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Field exists"
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Field does not exist"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Not sure if there is any speed difference... The 'in' thing is pretty handy for checking if something is in a list - in this case the ListFields is a list. For example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; a = [3,5,6,'kitty']&amp;nbsp;&amp;nbsp; # a Python list
&amp;gt;&amp;gt;&amp;gt; b = 2
&amp;gt;&amp;gt;&amp;gt; b in a
False
&amp;gt;&amp;gt;&amp;gt; c = 5
&amp;gt;&amp;gt;&amp;gt; c in a
True
&amp;gt;&amp;gt;&amp;gt; d = 'kitty'
&amp;gt;&amp;gt;&amp;gt; d in a
True
&amp;gt;&amp;gt;&amp;gt; 
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:11:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533258#M41774</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2021-12-11T23:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: Check for existence of a field</title>
      <link>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533259#M41775</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks all!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 04 Aug 2011 13:46:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533259#M41775</guid>
      <dc:creator>MikeDriver</dc:creator>
      <dc:date>2011-08-04T13:46:54Z</dc:date>
    </item>
    <item>
      <title>Re: Check for existence of a field</title>
      <link>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533260#M41776</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;An empty list evaluates to False so here's the way I do it:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
if arcpy.ListFields(tbl, field_name):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Field exists"
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Field doesn't exist"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It should be noted that AddField_management does not fail if you add a field that exists already, it just prints a warning and doesn't do anything. (Makes for less complicated ModelBuilder models.)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:11:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533260#M41776</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T23:11:22Z</dc:date>
    </item>
    <item>
      <title>Re: Check for existence of a field</title>
      <link>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533261#M41777</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is the most direct. Your code is so clear and concise. Well done!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Feb 2018 20:46:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533261#M41777</guid>
      <dc:creator>TylerCovington</dc:creator>
      <dc:date>2018-02-23T20:46:12Z</dc:date>
    </item>
    <item>
      <title>Re: Check for existence of a field</title>
      <link>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533262#M41778</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Stacy's arcpy.ListFields returns a list of field objects&amp;nbsp;(not field names)&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;so the "in" operator would not work. You could make it work by converting the list to field names (and converting to upper case because the&amp;nbsp;&lt;EM&gt;in&lt;/EM&gt; operator is case sensitive).&amp;nbsp;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;lstFields &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;ListFields&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
field_names &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;f&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;name&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;upper&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; f &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; lstFields&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"USNG"&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; field_names&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Field exists"&lt;/SPAN&gt; 
&lt;SPAN class="keyword token"&gt;else&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Field does not exist"&lt;/SPAN&gt;‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:11:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/533262#M41778</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T23:11:25Z</dc:date>
    </item>
    <item>
      <title>Re: Check for existence of a field</title>
      <link>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/1211971#M65537</link>
      <description>&lt;P&gt;Tested Stacy's code above and curtvprice is correct, the "in" operator will not work.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Sep 2022 13:27:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/1211971#M65537</guid>
      <dc:creator>Brownschuh</dc:creator>
      <dc:date>2022-09-13T13:27:56Z</dc:date>
    </item>
    <item>
      <title>Re: Check for existence of a field</title>
      <link>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/1211972#M65538</link>
      <description>&lt;P&gt;This code worked for me.&amp;nbsp; Using a couple for loops I was able to run this against multiple feature classes within multiple feature datasets to determine if a field was present.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Sep 2022 13:29:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/check-for-existence-of-a-field/m-p/1211972#M65538</guid>
      <dc:creator>Brownschuh</dc:creator>
      <dc:date>2022-09-13T13:29:18Z</dc:date>
    </item>
  </channel>
</rss>

