<?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: use python to create definition query in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623251#M48545</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the help. I tried running the script with the updates you made, and while the script runs all the way (doesn't receive any error messages), it doesn't actually apply the definition query. When I look at the newly clipped layer in ArcMap, it is clipped to the entire Ground Zones layer as opposed to just ground zone 1 or 2 for example.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, what is the function of the [0] at the end of the 6th and 7th lines?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 16 Jul 2013 12:55:40 GMT</pubDate>
    <dc:creator>JonathanCusick</dc:creator>
    <dc:date>2013-07-16T12:55:40Z</dc:date>
    <item>
      <title>use python to create definition query</title>
      <link>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623239#M48533</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi. Ive been trying to figure out how to accomplish this, but Ive run into some trouble. Im not sure if I am coding this correctly. My goal, overall, is to select features from a layer that intersect with features from another layer. The features in this second layer need to be defined by a definition query that I do not want to manually change. The selected features will then be exported to a new shapefile. I have been able to get the script to select and export, but I cannot get it to create the definition query. Any assistance will be greatly appreciated. Here is the code that I have, so far. Thanks!!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy

mxd = arcpy.mapping.MapDocument("C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Data/DataPrep.mxd")

for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name == "CountyCommission_TriCounty":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer.definitionQuery = "[DISTRICT] = '1' AND [COUNTY] = 'ORANGE'"

arcpy.SelectLayerByLocation_management("BusRoutes_Ridership","INTERSECT","CountyCommission_TriCounty","","NEW_SELECTION")

arcpy.CopyFeatures_management("BusRoutes_Ridership","C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Data/Orange/Or_Com1.shp")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 22 Mar 2011 12:46:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623239#M48533</guid>
      <dc:creator>RafiqBasaria</dc:creator>
      <dc:date>2011-03-22T12:46:57Z</dc:date>
    </item>
    <item>
      <title>Re: use python to create definition query</title>
      <link>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623240#M48534</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Been working on this more. I chaged a couple of parts, but am now running into the issue of how the layer is seeing the definition query. Since the layer reads it using single and double quotes, python cannot correctly enter the query. Here is the new code. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

mxd = arcpy.mapping.MapDocument("C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Data/DataPrep.mxd")

for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name == "CountyCommission_TriCounty":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = "DISTRICT" = '1' AND "COUNTY" = 'ORANGE'

arcpy.SelectLayerByLocation_management("BusRoutes_Ridership","INTERSECT","CountyCommission_TriCounty","","NEW_SELECTION")

arcpy.CopyFeatures_management("BusRoutes_Ridership","C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Data/Orange/Or_Com1.shp")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As you can see, the lyr.definitionQuery =&amp;nbsp; line is not correctly syntaxed. I cannot figure out how to get python to read that entire query definition as a string to insert into the definition query of the layer.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:33:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623240#M48534</guid>
      <dc:creator>RafiqBasaria</dc:creator>
      <dc:date>2021-12-12T02:33:15Z</dc:date>
    </item>
    <item>
      <title>Re: use python to create definition query</title>
      <link>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623241#M48535</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Just escape some of hte quotes and wrap it in more quotes:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;'"DISTRICT" = \'1\' AND "COUNTY" = \'ORANGE\''&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 22 Mar 2011 13:52:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623241#M48535</guid>
      <dc:creator>ChrisMathers</dc:creator>
      <dc:date>2011-03-22T13:52:30Z</dc:date>
    </item>
    <item>
      <title>Re: use python to create definition query</title>
      <link>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623242#M48536</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;That worked perfectly. Thanks!! I was unaware that this could be done. Thank you.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 22 Mar 2011 15:05:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623242#M48536</guid>
      <dc:creator>RafiqBasaria</dc:creator>
      <dc:date>2011-03-22T15:05:50Z</dc:date>
    </item>
    <item>
      <title>Re: use python to create definition query</title>
      <link>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623243#M48537</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Your welcome. All where clauses and definition queries need to be strings so sometimes escaping is needed to make it work.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 22 Mar 2011 16:12:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623243#M48537</guid>
      <dc:creator>ChrisMathers</dc:creator>
      <dc:date>2011-03-22T16:12:03Z</dc:date>
    </item>
    <item>
      <title>Re: use python to create definition query</title>
      <link>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623244#M48538</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;&lt;BLOCKQUOTE&gt;rbasaria;87366 wrote:&lt;BR /&gt;Hi. Ive been trying to figure out how to accomplish this, but Ive run into some trouble. Im not sure if I am coding this correctly. My goal, overall, is to select features from a layer that intersect with features from another layer. The features in this second layer need to be defined by a definition query that I do not want to manually change. The selected features will then be exported to a new shapefile. I have been able to get the script to select and export, but I cannot get it to create the definition query. Any assistance will be greatly appreciated. Here is the &lt;/BLOCKQUOTE&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Query = "[DISTRICT] = '1' AND [COUNTY] = 'ORANGE'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.MakeFeatureLayer(inFC,layername,Query)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Apr 2011 19:31:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623244#M48538</guid>
      <dc:creator>DaveVerbyla</dc:creator>
      <dc:date>2011-04-01T19:31:02Z</dc:date>
    </item>
    <item>
      <title>Re: use python to create definition query</title>
      <link>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623245#M48539</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;sql = "OWNER=" "'" +&amp;nbsp; "NuStar GP Holdings LLC" + "'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That's how I do it where Owner is the field name and NuStar GP Holdings LLC is the value...this is on SQL Server 2008..on Oracle I think it may differ a bit.&amp;nbsp; If I was going to use a variable for the value it would be something like..&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;sql = "OWNER=" "'" +&amp;nbsp; value_variable + "'"&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Oct 2011 15:09:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623245#M48539</guid>
      <dc:creator>justinperez</dc:creator>
      <dc:date>2011-10-17T15:09:16Z</dc:date>
    </item>
    <item>
      <title>Re: use python to create definition query</title>
      <link>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623246#M48540</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;sql = "OWNER='" +&amp;nbsp; "NuStar GP Holdings LLC" + "'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That's how I do it where Owner is the field name and NuStar GP Holdings LLC is the value...this is on SQL Server 2008..on Oracle I think it may differ a bit.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I want to vary the value...notice how I dropped the double quotes around the value&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;sql = "OWNER='" +&amp;nbsp; value_variable + "'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note the single quote after Owner= and also inside the last double quotes.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Oct 2011 15:14:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623246#M48540</guid>
      <dc:creator>justinperez</dc:creator>
      <dc:date>2011-10-17T15:14:11Z</dc:date>
    </item>
    <item>
      <title>Re: use python to create definition query</title>
      <link>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623247#M48541</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I've been trying to use a definition query just like rbasaria, and have used the syntax that clm42 suggested, but keep running into trouble. For now, I just want to be able to query out various zones from a larger map, and then run further geoprocessing functions on them.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy import env
import os

inzones = (.../RevisedGroundZones)
for lyr in inzones:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name == "RevisedGroundZones":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = ' "Grounds_GSC_Zones" = \'2\''&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When running it from the python window in ArcMap, I get an error message that says AttributeError: 'str' object has no attribute 'name' and when I run it as a script tool in ArcCatalog, I get an error saying that there is no definition Query function for 'lyr'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help would be very appreciated, thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:33:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623247#M48541</guid>
      <dc:creator>JonathanCusick</dc:creator>
      <dc:date>2021-12-12T02:33:18Z</dc:date>
    </item>
    <item>
      <title>Re: use python to create definition query</title>
      <link>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623248#M48542</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I've been trying to use a definition query just like rbasaria, and have used the syntax that clm42 suggested, but keep running into trouble. For now, I just want to be able to query out various zones from a larger map, and then run further geoprocessing functions on them.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy import env
import os

inzones = (.../RevisedGroundZones)
for lyr in inzones:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name == "RevisedGroundZones":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = ' "Grounds_GSC_Zones" = \'2\''&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;When running it from the python window in ArcMap, I get an error message that says AttributeError: 'str' object has no attribute 'name' and when I run it as a script tool in ArcCatalog, I get an error saying that there is no definition Query function for 'lyr'&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Any help would be very appreciated, thanks!&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Can you post the rest of your code? How you create the mxd object etc. You need to make a layer object to get the name property from it if you are running this out of process.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:33:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623248#M48542</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-12T02:33:20Z</dc:date>
    </item>
    <item>
      <title>Re: use python to create definition query</title>
      <link>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623249#M48543</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ah I didn't realize I had to do so. That's all of the code referring to the query. The rest of it begins to run the clip function along with others down the line. So I have to define the mxd that this is happening in in order to call upon the attributes of the Ground Zones feature class? Is there a way to run this just in Catalog?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried defining the mxd as shown in the post at the top of the thread, but am still having problems. Whenever I try to set mxd to the full pathname of the map, I get an error saying Invalid MXD Filename. When just using the keyword "CURRENT" to define the mxd, it at least ran, but then I got that same 'str' object has no attribute 'name'.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy import env
import os

mxd = arcpy.mapping.MapDocument("CURRENT")
inzones = (.../RevisedGroundZones)
for lyr in inzones:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name == "RevisedGroundZones":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = ' "Grounds_GSC_Zones" = \'2\''&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:33:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623249#M48543</guid>
      <dc:creator>JonathanCusick</dc:creator>
      <dc:date>2021-12-12T02:33:23Z</dc:date>
    </item>
    <item>
      <title>Re: use python to create definition query</title>
      <link>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623250#M48544</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Ah I didn't realize I had to do so. That's all of the code referring to the query. The rest of it begins to run the clip function along with others down the line. So I have to define the mxd that this is happening in in order to call upon the attributes of the Ground Zones feature class? Is there a way to run this just in Catalog?&lt;BR /&gt;&lt;BR /&gt;I tried defining the mxd as shown in the post at the top of the thread, but am still having problems. Whenever I try to set mxd to the full pathname of the map, I get an error saying Invalid MXD Filename. When just using the keyword "CURRENT" to define the mxd, it at least ran, but then I got that same 'str' object has no attribute 'name'.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy import env
import os

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "RevisedGroundZones", df)[0]
lyr.definitionQuery = ' "Grounds_GSC_Zones" = \'2\''&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Think you just need to define the layer.&amp;nbsp; If it is just one layer, no need to iterate.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;R_&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:33:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623250#M48544</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2021-12-12T02:33:26Z</dc:date>
    </item>
    <item>
      <title>Re: use python to create definition query</title>
      <link>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623251#M48545</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the help. I tried running the script with the updates you made, and while the script runs all the way (doesn't receive any error messages), it doesn't actually apply the definition query. When I look at the newly clipped layer in ArcMap, it is clipped to the entire Ground Zones layer as opposed to just ground zone 1 or 2 for example.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, what is the function of the [0] at the end of the 6th and 7th lines?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 16 Jul 2013 12:55:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623251#M48545</guid>
      <dc:creator>JonathanCusick</dc:creator>
      <dc:date>2013-07-16T12:55:40Z</dc:date>
    </item>
    <item>
      <title>Re: use python to create definition query</title>
      <link>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623252#M48546</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;

import arcpy
from arcpy import env
import os


mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "RevisedGroundZones", df)[0]
lyr.definitionQuery = ' "Grounds_GSC_Zones" = \'2\''

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, similar to your code, could do it this way as well if you wanted to iterate through the list instead of just grabbing the first item (normally first of one):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
for lyr in arcpy.mapping.ListLayers(mxd, "*"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name == "RevisedGroundZones":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = ' "Grounds_GSC_Zones" = \'2\''

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The "List"Layers and "List"DataFrames create a list of matching objects, so, all Dataframes with name "Layers" would be appended to the df list, and all layers, within the specified dataframe, that match "RevisedGroundZones" would be appended to the lyr list.&amp;nbsp; However, putting the [0] actually makes it grab the first item in the list.&amp;nbsp; I am assuming here that you only have one dataframe named "Layers" and one layer within that named "RevisedGroundZones".&amp;nbsp; That way, there would only be one item in each list, so putting the [0] on the end grabs it without having to iterate through a list of one.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have successfully ran this and it applies the definition query (of course, this is for text field, if your RevisedGroundZones attribute is a number field, the quotes need to dissapear from the definition query.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, if you only have one dataframe name "Layers" (or "layers" as list function here is not case sensitive), and within that only one layer named RevisedGroundZones, it should be applying the definition query.&amp;nbsp; If you have more than one with this name, it would only apply to the first on in the list.&amp;nbsp; Would then need to iterate through if you wanted it applied to all with that name.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Of course, you can always throw a mxd.save() in there after the definition query, then open the mxd with arc map and see what the actual def query being applied is (and if there are any drawing errors related).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Once you get the def query working, then let it move on to the clipping.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;R_&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:33:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/use-python-to-create-definition-query/m-p/623252#M48546</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2021-12-12T02:33:29Z</dc:date>
    </item>
  </channel>
</rss>

