<?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: List Comprehensions in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/list-comprehensions/m-p/22761#M779</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; I'm working on a longer-form version of this for a blog post, but here's the gist:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;The Map Document object lives in the arcpy.mapping module. To create a new Map Document object is easy, simply call arcpy.mapping.MapDocument with the path to the MXD you wish to use:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;mxd = arcpy.mapping.MapDocument('d:\\path\\to\\map.mxd')&lt;BR /&gt;&lt;/SPAN&gt; &lt;BR /&gt;Something to note about python strings: backslashes need to be either escaped like&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;'d:\\path\\to\\map.mxd'&lt;/SPAN&gt; or the string can be flagged as a raw string with the r flag like&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;r'd:\\path\\to\\map.mxd'&lt;/SPAN&gt;.&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;In these examples Iâ??ll be using the Python window in ArcGIS Desktop. The Map document object also accepts the special path string â??currentâ?? in desktop only that lets you manipulate the current document in the window:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;mxd = arcpy.mapping.MapDocument('current')&lt;/SPAN&gt; &lt;BR /&gt; &lt;BR /&gt;From here the interactive window is a great resource to learn and experiment. I can get an idea of what type of properties a map document has by typing in the name of my variable, hitting dot, and see what I have access to.&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;This makes prototyping and exploring Python code easy, because of the immediate nature of the feedback given in the interactive window. I get a result as soon second I press enter, without needing to save or compile any code.&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;Layers can be accessed from .lyr files or from the TOC in a Map document. To load from a layer file:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;lyr = arcpy.mapping.MapDocument('d:\\path\\to\\layer.lyr')&lt;/SPAN&gt; &lt;BR /&gt; &lt;BR /&gt;Layers can be retrieved from a map document by using the arcpy.mapping.ListLayers function as well. In my interatice session I have a simple MXD with some OpenStreetMap data in it, and from the Python window&amp;nbsp; &lt;BR /&gt; I issue the ListLayers function and get:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;&amp;gt;&amp;gt;&amp;gt; arcpy.mapping.ListLayers(mxd)&lt;BR /&gt;[&amp;lt;map group layer u'OpenStreetMap data'&amp;gt;, &amp;lt;map layer u'california_poi'&amp;gt;, &amp;lt;map layer u'california_highway'&amp;gt;, &amp;lt;map layer u'california_natural'&amp;gt;, &amp;lt;map layer u'states'&amp;gt;]&lt;/SPAN&gt; &lt;BR /&gt; &lt;BR /&gt;Note this is different than the list of strings in functions such as ListDatasets, it is actual layer objects. If I just want a list of the names from these objects, I can employ a list comprehension:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;&amp;gt;&amp;gt;&amp;gt; [lyr.longName for lyr in arcpy.mapping.ListLayers(mxd)]&lt;BR /&gt; [u'OpenStreetMap data', u'OpenStreetMap data\\california_poi', u'OpenStreetMap data\\california_highway', u'OpenStreetMap data\\california_natural', u'states']&lt;/SPAN&gt; &lt;BR /&gt; &lt;BR /&gt;List comprehensions are a shortcut built into Python for a common type of for loop where one loops over a list of values to produce a new list of values. In this case, weâ??re grabbing the longName attribute from each value in the list returned from ListLayers. The general formula for a list comprehension is:&amp;nbsp; &lt;BR /&gt;Simple:&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;[expression for value in list]&lt;/SPAN&gt; &lt;BR /&gt;With conditional:&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;[expression for value in list if condition]&lt;/SPAN&gt; &lt;BR /&gt; &lt;BR /&gt;The&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;if condition&lt;/SPAN&gt; form allows one to not only map values, but also apply a conditional filter and only include certain values in the new list. For examples, I only want all the layer names that are feature layers:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;&amp;gt;&amp;gt;&amp;gt; [lyr.longName for lyr in arcpy.mapping.ListLayers(mxd) if lyr.isFeatureLayer]&lt;BR /&gt;[u'OpenStreetMap data\\california_poi', u'OpenStreetMap data\\california_highway', u'OpenStreetMap data\\california_natural', u'states']&lt;BR /&gt;&lt;/SPAN&gt; &lt;BR /&gt; &lt;BR /&gt;Note the group layer named OpenStreetMap data is no longer included in the results. This also makes it possible to fetch a layer by a specific name and only work with it:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;&amp;gt;&amp;gt;&amp;gt; lyrlist = [lyr for lyr in arcpy.mapping.ListLayers(mxd) if lyr.name == "california_highway"]&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; lyrlist&lt;BR /&gt;[&amp;lt;map layer u'california_highway'&amp;gt;]&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; if lyrlist:&lt;BR /&gt;... lyr = lyrlist.pop() # pop takes one element out of end of the list and returns it&lt;BR /&gt;... &lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; lyr&lt;BR /&gt;&amp;lt;map layer u'california_highway'&amp;gt;&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; lyr.name&lt;BR /&gt;u'california_highway'&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; lyr.dataSource&lt;BR /&gt;u'C:\\DATA\\openstreetmap.gdb\\california_highway'&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; lyr.transparency&lt;BR /&gt;0&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; lyr.transparency = 10&lt;/SPAN&gt; &lt;BR /&gt; &lt;BR /&gt;Many common scenarios for finding and manipulating layers can be accomplished using the built-in functions in arcpy.mapping and a little bit of manipulation with a list comprehension.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;By chance has there been a blog entry going over this a bit more?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 10 May 2010 20:14:29 GMT</pubDate>
    <dc:creator>TedCronin</dc:creator>
    <dc:date>2010-05-10T20:14:29Z</dc:date>
    <item>
      <title>List Comprehensions</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/list-comprehensions/m-p/22758#M776</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;To Jason --&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So, awhile back when I spoke to you on the phone about ListLayers and how they were objects rather than strings, you mentioned that I could use a List Comprehension to achieve my results.&amp;nbsp; I am wondering if you could post some sample code of how to use list comprehensions with arcpy.&amp;nbsp; Even perhaps as a blog post.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Jan 2010 16:26:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/list-comprehensions/m-p/22758#M776</guid>
      <dc:creator>TedCronin</dc:creator>
      <dc:date>2010-01-13T16:26:08Z</dc:date>
    </item>
    <item>
      <title>List Comprehensions</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/list-comprehensions/m-p/22759#M777</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm working on a longer-form version of this for a blog post, but here's the gist:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The Map Document object lives in the arcpy.mapping module. To create a new Map Document object is easy, simply call arcpy.mapping.MapDocument with the path to the MXD you wish to use:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;mxd = arcpy.mapping.MapDocument('d:\\path\\to\\map.mxd')&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Something to note about python strings: backslashes need to be either escaped like 'd:\\path\\to\\map.mxd' or the string can be flagged as a raw string with the r flag like r'd:\\path\\to\\map.mxd'.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In these examples Iâ??ll be using the Python window in ArcGIS Desktop. The Map document object also accepts the special path string â??currentâ?? in desktop only that lets you manipulate the current document in the window:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;mxd = arcpy.mapping.MapDocument('current')&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;From here the interactive window is a great resource to learn and experiment. I can get an idea of what type of properties a map document has by typing in the name of my variable, hitting dot, and see what I have access to.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This makes prototyping and exploring Python code easy, because of the immediate nature of the feedback given in the interactive window. I get a result as soon second I press enter, without needing to save or compile any code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Layers can be accessed from .lyr files or from the TOC in a Map document. To load from a layer file:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;lyr = arcpy.mapping.MapDocument('d:\\path\\to\\layer.lyr')&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Layers can be retrieved from a map document by using the arcpy.mapping.ListLayers function as well. In my interatice session I have a simple MXD with some OpenStreetMap data in it, and from the Python window&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; I issue the ListLayers function and get:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; arcpy.mapping.ListLayers(mxd)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[&amp;lt;map group layer u'OpenStreetMap data'&amp;gt;, &amp;lt;map layer u'california_poi'&amp;gt;, &amp;lt;map layer u'california_highway'&amp;gt;, &amp;lt;map layer u'california_natural'&amp;gt;, &amp;lt;map layer u'states'&amp;gt;]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note this is different than the list of strings in functions such as ListDatasets, it is actual layer objects. If I just want a list of the names from these objects, I can employ a list comprehension:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; [lyr.longName for lyr in arcpy.mapping.ListLayers(mxd)]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; [u'OpenStreetMap data', u'OpenStreetMap data\\california_poi', u'OpenStreetMap data\\california_highway', u'OpenStreetMap data\\california_natural', u'states']&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;List comprehensions are a shortcut built into Python for a common type of for loop where one loops over a list of values to produce a new list of values. In this case, weâ??re grabbing the longName attribute from each value in the list returned from ListLayers. The general formula for a list comprehension is:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Simple: [expression for value in list]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;With conditional: [expression for value in list if condition]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The if condition form allows one to not only map values, but also apply a conditional filter and only include certain values in the new list. For examples, I only want all the layer names that are feature layers:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; [lyr.longName for lyr in arcpy.mapping.ListLayers(mxd) if lyr.isFeatureLayer]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[u'OpenStreetMap data\\california_poi', u'OpenStreetMap data\\california_highway', u'OpenStreetMap data\\california_natural', u'states']&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note the group layer named OpenStreetMap data is no longer included in the results. This also makes it possible to fetch a layer by a specific name and only work with it:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; lyrlist = [lyr for lyr in arcpy.mapping.ListLayers(mxd) if lyr.name == "california_highway"]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; lyrlist&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[&amp;lt;map layer u'california_highway'&amp;gt;]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; if lyrlist:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;... lyr = lyrlist.pop() # pop takes one element out of end of the list and returns it&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;... &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; lyr&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;lt;map layer u'california_highway'&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; lyr.name&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;u'california_highway'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; lyr.dataSource&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;u'C:\\DATA\\openstreetmap.gdb\\california_highway'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; lyr.transparency&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; lyr.transparency = 10&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Many common scenarios for finding and manipulating layers can be accomplished using the built-in functions in arcpy.mapping and a little bit of manipulation with a list comprehension.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 16 Jan 2010 21:54:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/list-comprehensions/m-p/22759#M777</guid>
      <dc:creator>JasonScheirer</dc:creator>
      <dc:date>2010-01-16T21:54:43Z</dc:date>
    </item>
    <item>
      <title>Cool</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/list-comprehensions/m-p/22760#M778</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you Jason.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Jan 2010 17:58:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/list-comprehensions/m-p/22760#M778</guid>
      <dc:creator>TedCronin</dc:creator>
      <dc:date>2010-01-19T17:58:37Z</dc:date>
    </item>
    <item>
      <title>Re: List Comprehensions</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/list-comprehensions/m-p/22761#M779</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; I'm working on a longer-form version of this for a blog post, but here's the gist:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;The Map Document object lives in the arcpy.mapping module. To create a new Map Document object is easy, simply call arcpy.mapping.MapDocument with the path to the MXD you wish to use:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;mxd = arcpy.mapping.MapDocument('d:\\path\\to\\map.mxd')&lt;BR /&gt;&lt;/SPAN&gt; &lt;BR /&gt;Something to note about python strings: backslashes need to be either escaped like&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;'d:\\path\\to\\map.mxd'&lt;/SPAN&gt; or the string can be flagged as a raw string with the r flag like&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;r'd:\\path\\to\\map.mxd'&lt;/SPAN&gt;.&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;In these examples Iâ??ll be using the Python window in ArcGIS Desktop. The Map document object also accepts the special path string â??currentâ?? in desktop only that lets you manipulate the current document in the window:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;mxd = arcpy.mapping.MapDocument('current')&lt;/SPAN&gt; &lt;BR /&gt; &lt;BR /&gt;From here the interactive window is a great resource to learn and experiment. I can get an idea of what type of properties a map document has by typing in the name of my variable, hitting dot, and see what I have access to.&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;This makes prototyping and exploring Python code easy, because of the immediate nature of the feedback given in the interactive window. I get a result as soon second I press enter, without needing to save or compile any code.&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;Layers can be accessed from .lyr files or from the TOC in a Map document. To load from a layer file:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;lyr = arcpy.mapping.MapDocument('d:\\path\\to\\layer.lyr')&lt;/SPAN&gt; &lt;BR /&gt; &lt;BR /&gt;Layers can be retrieved from a map document by using the arcpy.mapping.ListLayers function as well. In my interatice session I have a simple MXD with some OpenStreetMap data in it, and from the Python window&amp;nbsp; &lt;BR /&gt; I issue the ListLayers function and get:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;&amp;gt;&amp;gt;&amp;gt; arcpy.mapping.ListLayers(mxd)&lt;BR /&gt;[&amp;lt;map group layer u'OpenStreetMap data'&amp;gt;, &amp;lt;map layer u'california_poi'&amp;gt;, &amp;lt;map layer u'california_highway'&amp;gt;, &amp;lt;map layer u'california_natural'&amp;gt;, &amp;lt;map layer u'states'&amp;gt;]&lt;/SPAN&gt; &lt;BR /&gt; &lt;BR /&gt;Note this is different than the list of strings in functions such as ListDatasets, it is actual layer objects. If I just want a list of the names from these objects, I can employ a list comprehension:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;&amp;gt;&amp;gt;&amp;gt; [lyr.longName for lyr in arcpy.mapping.ListLayers(mxd)]&lt;BR /&gt; [u'OpenStreetMap data', u'OpenStreetMap data\\california_poi', u'OpenStreetMap data\\california_highway', u'OpenStreetMap data\\california_natural', u'states']&lt;/SPAN&gt; &lt;BR /&gt; &lt;BR /&gt;List comprehensions are a shortcut built into Python for a common type of for loop where one loops over a list of values to produce a new list of values. In this case, weâ??re grabbing the longName attribute from each value in the list returned from ListLayers. The general formula for a list comprehension is:&amp;nbsp; &lt;BR /&gt;Simple:&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;[expression for value in list]&lt;/SPAN&gt; &lt;BR /&gt;With conditional:&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;[expression for value in list if condition]&lt;/SPAN&gt; &lt;BR /&gt; &lt;BR /&gt;The&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;if condition&lt;/SPAN&gt; form allows one to not only map values, but also apply a conditional filter and only include certain values in the new list. For examples, I only want all the layer names that are feature layers:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;&amp;gt;&amp;gt;&amp;gt; [lyr.longName for lyr in arcpy.mapping.ListLayers(mxd) if lyr.isFeatureLayer]&lt;BR /&gt;[u'OpenStreetMap data\\california_poi', u'OpenStreetMap data\\california_highway', u'OpenStreetMap data\\california_natural', u'states']&lt;BR /&gt;&lt;/SPAN&gt; &lt;BR /&gt; &lt;BR /&gt;Note the group layer named OpenStreetMap data is no longer included in the results. This also makes it possible to fetch a layer by a specific name and only work with it:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;SPAN style="font-family:&amp;quot;Courier New&amp;quot;;"&gt;&amp;gt;&amp;gt;&amp;gt; lyrlist = [lyr for lyr in arcpy.mapping.ListLayers(mxd) if lyr.name == "california_highway"]&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; lyrlist&lt;BR /&gt;[&amp;lt;map layer u'california_highway'&amp;gt;]&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; if lyrlist:&lt;BR /&gt;... lyr = lyrlist.pop() # pop takes one element out of end of the list and returns it&lt;BR /&gt;... &lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; lyr&lt;BR /&gt;&amp;lt;map layer u'california_highway'&amp;gt;&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; lyr.name&lt;BR /&gt;u'california_highway'&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; lyr.dataSource&lt;BR /&gt;u'C:\\DATA\\openstreetmap.gdb\\california_highway'&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; lyr.transparency&lt;BR /&gt;0&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; lyr.transparency = 10&lt;/SPAN&gt; &lt;BR /&gt; &lt;BR /&gt;Many common scenarios for finding and manipulating layers can be accomplished using the built-in functions in arcpy.mapping and a little bit of manipulation with a list comprehension.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;By chance has there been a blog entry going over this a bit more?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 May 2010 20:14:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/list-comprehensions/m-p/22761#M779</guid>
      <dc:creator>TedCronin</dc:creator>
      <dc:date>2010-05-10T20:14:29Z</dc:date>
    </item>
  </channel>
</rss>

