<?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: extract hex codes from layer symbology using arcpy loop in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374510#M69721</link>
    <description>&lt;P&gt;I'm not quite sure what you mean by combine. Do you want to also get the colors from a symbology with grouped unique values? Or do you have layers with grouped values from multiple fields? Eh, let's do both...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To extract the item dict for different combinations of (multiple fields) and (grouped values), we have to generalize a bit. Let's take a look at how these combinations are represented in Python:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# 4 layers with different Unique Value symbologies
layer_names = [
    "unique",  # just a plain UV symbology
    "unique_grouped",  # UV from a single field, but in groups
    "unique_multiple", # UV from multiple fields, ungrouped
    "unique_multiple_grouped",  # UV from multiple fields, grouped
    ]
aprx = arcpy.mp.ArcGISProject("current")

for name in layer_names:
    layer = aprx.activeMap.listLayers(name)[0]
    renderer = layer.symbology.renderer
    first_item = renderer.groups[0].items[0]
    print(f"layer:\t{layer.name}\nfields:\t{renderer.fields}\nvalues:\t{first_item.values}\n\n")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;layer:	unique
fields:	['TextField1']
values:	[['A']]


layer:	unique_grouped
fields:	['TextField1']
values:	[['A'], ['B']]


layer:	unique_multiple
fields:	['TextField1', 'IntegerField1']
values:	[['A', '12345']]


layer:	unique_multiple_grouped
fields:	['TextField1', 'IntegerField1']
values:	[['A', '12345'], ['C', '2']]&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;values is a list of lists. There is an inner list for each value in the group. In these lists, there is a (string!) value for each field.&lt;/P&gt;&lt;P&gt;We want to use these lists as dictionary keys, but that's impossible, because keys have to be immutable, and lists are mutable, so we change them to immutable tuples. Also, we have to have a separate key for each value in a group.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def get_item_dict(symbology):
    if symbology.renderer.type != "UniqueValueRenderer":
        raise ValueError("symbology has to be Unique Value!")
    item_dict = dict()
    for group in symbology.renderer.groups:
        for item in group.items:
            for value_group in item.values:
                item_dict[tuple(value_group)] = item
    return item_dict&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, let's put the color conversion into a separate function to make the code a bit cleaner...&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def convert_to_hex(color):
    color_type = list(color.keys())[0]
    if color_type == "RGB":
        rgb = color["RGB"][:3]
        return "#%02x%02x%02x" % tuple(rgb)
    else:
        return None&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And with these two functions, we can concentrate on the actual workflow:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;layer_names = ["TestPolygons"]
aprx = arcpy.mp.ArcGISProject("current")

for name in layer_names:
    # get the layer
    layer = aprx.activeMap.listLayers(name)[0]
    # create a dict {values: item}
    item_dict = get_item_dict(layer.symbology)
    # add a new field
    arcpy.management.AddField(layer, "PW_Zoning_Designation", "TEXT")
    arcpy.management.AddField(layer, "ColorRaw", "TEXT")
    arcpy.management.AddField(layer, "ColorHex", "TEXT")
    
    # insert the label and color values
    with arcpy.da.UpdateCursor(layer, ["PW_Zoning_Designation", "ColorRaw", "ColorHex"] + layer.symbology.renderer.fields) as cursor:
        for row in cursor:
            # get the item
            key = tuple([str(x) for x in row[3:]])  # item.values stores values as string
            try:
                item = item_dict[key]
            # no item found -&amp;gt; default value and skip to the next feature
            except KeyError:
                row[0] = "No  Symbol"
                cursor.updateRow(row)
                continue
            # store the item's label
            row[0] = item.label
            # store the raw color
            row[1] = str(item.symbol.color)
            # store the hex value
            row[2] = convert_to_hex(item.symbol.color)
            # write values
            cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 25 Jan 2024 17:37:38 GMT</pubDate>
    <dc:creator>JohannesLindner</dc:creator>
    <dc:date>2024-01-25T17:37:38Z</dc:date>
    <item>
      <title>extract hex codes from layer symbology using arcpy loop</title>
      <link>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374092#M69712</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am a GIS Analyst/Urban Planner working with land use data. I have a standard land use symbology for use in different cities and counties.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to extract the hex codes from the layer symbology and add them as a new field. The script from this &lt;A href="https://community.esri.com/t5/python-questions/get-list-of-layer-s-symbology-classes-and-symbol/td-p/1238601" target="_self"&gt;solution post&lt;/A&gt; by&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/294341"&gt;@JohannesLindner&lt;/a&gt;&amp;nbsp;. The script works to add the label of my symbology layer to a new field:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy&lt;/LI-CODE&gt;&lt;LI-CODE lang="python"&gt;layer_names = ["PW Zoning"]
aprx = arcpy.mp.ArcGISProject("current")

for name in layer_names:
    # get the layer
    layer = aprx.activeMap.listLayers(name)[0]
    # get the symbology fields and items
    renderer = layer.symbology.renderer
    fields = renderer.fields
    items = renderer.groups[0].items
    # create a dict {values: label}
    item_dict = {tuple(i.values[0]): i.label for i in items}
    # add a new field
    arcpy.management.AddField(layer, "PW_Zoning_Designation", "TEXT")
    # insert the label values
    with arcpy.da.UpdateCursor(layer, ["PW_Zoning_Designation"] + fields) as cursor:
        for row in cursor:
            key = tuple([str(x) for x in row[1:]])  # item.values stores values as string
            try:
                row[0] = item_dict[key]
                cursor.updateRow(row)
            except KeyError:  # no symbology found
                pass&lt;/LI-CODE&gt;&lt;P&gt;I am curious how I would add the hex codes into this code to add a new field that would include the six-digit hex codes; RGB codes work too. Thanks for any help. I am new to arcpy and will be attending the Developer Conference in two months.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="symbologyHex.png" style="width: 753px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/92688i09890D0B21444B5B/image-size/large?v=v2&amp;amp;px=999" role="button" title="symbologyHex.png" alt="symbologyHex.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jan 2024 20:08:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374092#M69712</guid>
      <dc:creator>ChrisGiamarino</dc:creator>
      <dc:date>2024-01-24T20:08:56Z</dc:date>
    </item>
    <item>
      <title>Re: extract hex codes from layer symbology using arcpy loop</title>
      <link>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374177#M69714</link>
      <description>&lt;P&gt;As it turns out, getting a symbol's color is easy, but there's no built-in way to get the hex value...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you have defined all your symbol colors in the RGB system, you're good to go:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;layer_names = ["TestPolygons"]
aprx = arcpy.mp.ArcGISProject("current")

for name in layer_names:
    # get the layer
    layer = aprx.activeMap.listLayers(name)[0]
    # get the symbology fields and items
    renderer = layer.symbology.renderer
    fields = renderer.fields
    items = renderer.groups[0].items
    # create a dict {values: iteml}
    item_dict = {tuple(item.values[0]): item for item in items}
    # add a new field
    arcpy.management.AddField(layer, "PW_Zoning_Designation", "TEXT")
    arcpy.management.AddField(layer, "ColorRaw", "TEXT")
    arcpy.management.AddField(layer, "ColorHex", "TEXT")
    
    # insert the label and color values
    with arcpy.da.UpdateCursor(layer, ["PW_Zoning_Designation", "ColorRaw", "ColorHex"] + fields) as cursor:
        for row in cursor:
            # get the item
            key = tuple([str(x) for x in row[3:]])  # item.values stores values as string
            try:
                item = item_dict[key]
            # no item found -&amp;gt; default value and skip to the next feature
            except KeyError:
                row[0] = "No  Symbol"
                cursor.updateRow(row)
                continue
            # store the item's label
            row[0] = item.label
            # get the symbol color
            color = item.symbol.color
            color_type = list(color.keys())[0]
            # if it's rgb, convert to hex
            if color_type == "RGB":
                rgb = color["RGB"][:3]
                row[2] = '#%02x%02x%02x' % tuple(rgb)
            # if it's something else (hsv, hsl, cmyk), you have to convert that in other ways... here I just don't convert
            else:
                row[2] = None
            # store the color value as string for documentation
            row[1] = str(color)
            # write values
            cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note that we're storing the whole symbology item in the dictionary (line 12), not only the label. This way, we can access the item's symbol color in the for-loop (line 33).&lt;/P&gt;&lt;P&gt;Colors can be defined in different systems (RGB, HSV, HSL, CMYK, ...). Converting from RGB to hex is pretty straightforward (line 38). If you have other color systems, you probably have to convert those values into RGB first (line 41). A good start could be the module colorsys, which is part of the default Python installation. It might be easier to go through your symbology and just switch the system there.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_0-1706133979264.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/92725iDA506B293E2A53BE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_0-1706133979264.png" alt="JohannesLindner_0-1706133979264.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_1-1706134019351.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/92726iD31925A40F4A86F8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_1-1706134019351.png" alt="JohannesLindner_1-1706134019351.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jan 2024 22:08:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374177#M69714</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2024-01-24T22:08:44Z</dc:date>
    </item>
    <item>
      <title>Re: extract hex codes from layer symbology using arcpy loop</title>
      <link>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374221#M69715</link>
      <description>&lt;P&gt;whoa! thank you so much&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/294341"&gt;@JohannesLindner&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;one last quick question...when I run the initial code on Grouped Values, it only searches for the first value in the group and applies the new Label. See screenshot...how can I get the initial code to search through each land use code after the semi-colon and add the new label?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="landUse_loop.png" style="width: 785px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/92738i7B6D0CF403D16059/image-size/large?v=v2&amp;amp;px=999" role="button" title="landUse_loop.png" alt="landUse_loop.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;sample code again:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy 

layer_names = ["ZoningPlacerCo"]
aprx = arcpy.mp.ArcGISProject("current")

for name in layer_names:
    # get the layer
    layer = aprx.activeMap.listLayers(name)[0]
    # get the symbology fields and items
    renderer = layer.symbology.renderer
    fields = renderer.fields
    items = renderer.groups[0].items
    # create a dict {values: label}
    item_dict = {tuple(i.values[0]): i.label for i in items}
    # add a new field
    arcpy.management.AddField(layer, "LandUseDesignation1", "TEXT")
    # insert the label values
    with arcpy.da.UpdateCursor(layer, ["LandUseDesignation1"] + fields) as cursor:
        for row in cursor:
            key = tuple([str(x) for x in row[1:]])  # item.values stores values as string
            try:
                row[0] = item_dict[key]
                cursor.updateRow(row)
            except KeyError:  # no symbology found
                pass&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 25 Jan 2024 00:32:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374221#M69715</guid>
      <dc:creator>ChrisGiamarino</dc:creator>
      <dc:date>2024-01-25T00:32:19Z</dc:date>
    </item>
    <item>
      <title>Re: extract hex codes from layer symbology using arcpy loop</title>
      <link>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374337#M69716</link>
      <description>&lt;P&gt;The original script was for Unique Values from multiple fields. You have Unique Values from one field in groups. These are represented differently in Python.&lt;/P&gt;&lt;P&gt;This should work:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;layer_names = ["TestPolygons"]
aprx = arcpy.mp.ArcGISProject("current")

for name in layer_names:
    # get the layer
    layer = aprx.activeMap.listLayers(name)[0]
    # get the symbology fields and items
    renderer = layer.symbology.renderer
    fields = renderer.fields
    items = renderer.groups[0].items
    # create a dict {values: label}
    item_dict = dict()
    for item in items:
        for v in item.values:
            item_dict[v[0]] = item.label
    # add a new field
    arcpy.management.AddField(layer, "LandUseDesignation1", "TEXT")
    # insert the label values
    with arcpy.da.UpdateCursor(layer, ["LandUseDesignation1"] + fields) as cursor:
        for row in cursor:
            key = row[-1]
            try:
                row[0] = item_dict[key]
                cursor.updateRow(row)
            except KeyError:  # no symbology found
                pass&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We have to change how we build the item_dict (lines 12-15) and how&amp;nbsp; to get values from it (line 21).&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jan 2024 13:26:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374337#M69716</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2024-01-25T13:26:07Z</dc:date>
    </item>
    <item>
      <title>Re: extract hex codes from layer symbology using arcpy loop</title>
      <link>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374427#M69717</link>
      <description>&lt;P&gt;epic! thank you. I am familiar with geopandas and folium. arcpy is new to me. I appreciate your explanations and comments (#)&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jan 2024 15:57:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374427#M69717</guid>
      <dc:creator>ChrisGiamarino</dc:creator>
      <dc:date>2024-01-25T15:57:45Z</dc:date>
    </item>
    <item>
      <title>Re: extract hex codes from layer symbology using arcpy loop</title>
      <link>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374432#M69718</link>
      <description>&lt;P&gt;sorry, quick question &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/294341"&gt;@JohannesLindner&lt;/a&gt;&amp;nbsp;. how would you combine adding the color code with multiple fields with this new code that searches for grouped values?&amp;nbsp;&lt;/P&gt;&lt;P&gt;specifically updating the code below with the item_dict (lines 12-15) and line 21:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;layer_names = ["TestPolygons"]
aprx = arcpy.mp.ArcGISProject("current")

for name in layer_names:
    # get the layer
    layer = aprx.activeMap.listLayers(name)[0]
    # get the symbology fields and items
    renderer = layer.symbology.renderer
    fields = renderer.fields
    items = renderer.groups[0].items
    # create a dict {values: iteml}
    item_dict = {tuple(item.values[0]): item for item in items}
    # add a new field
    arcpy.management.AddField(layer, "PW_Zoning_Designation", "TEXT")
    arcpy.management.AddField(layer, "ColorRaw", "TEXT")
    arcpy.management.AddField(layer, "ColorHex", "TEXT")
    
    # insert the label and color values
    with arcpy.da.UpdateCursor(layer, ["PW_Zoning_Designation", "ColorRaw", "ColorHex"] + fields) as cursor:
        for row in cursor:
            # get the item
            key = tuple([str(x) for x in row[3:]])  # item.values stores values as string
            try:
                item = item_dict[key]
            # no item found -&amp;gt; default value and skip to the next feature
            except KeyError:
                row[0] = "No  Symbol"
                cursor.updateRow(row)
                continue
            # store the item's label
            row[0] = item.label
            # get the symbol color
            color = item.symbol.color
            color_type = list(color.keys())[0]
            # if it's rgb, convert to hex
            if color_type == "RGB":
                rgb = color["RGB"][:3]
                row[2] = '#%02x%02x%02x' % tuple(rgb)
            # if it's something else (hsv, hsl, cmyk), you have to convert that in other ways... here I just don't convert
            else:
                row[2] = None
            # store the color value as string for documentation
            row[1] = str(color)
            # write values
            cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jan 2024 16:13:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374432#M69718</guid>
      <dc:creator>ChrisGiamarino</dc:creator>
      <dc:date>2024-01-25T16:13:56Z</dc:date>
    </item>
    <item>
      <title>Re: extract hex codes from layer symbology using arcpy loop</title>
      <link>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374483#M69720</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/294341"&gt;@JohannesLindner&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;here is my sample code trying to iterate through groups and apply color column value attributes. I am getting an index error saying 'IndexError: list index out of range'&lt;/P&gt;&lt;LI-CODE lang="python"&gt;layer_names = ["ZoningPlacerCo"]
aprx = arcpy.mp.ArcGISProject("current")

for name in layer_names:
    # get the layer
    layer = aprx.activeMap.listLayers(name)[0]
    # get the symbology fields and items
    renderer = layer.symbology.renderer
    fields = renderer.fields
    items = renderer.groups[0].items
    # create a dict {values: iteml}
    item_dict = dict()
    for item in items:
        for v in item.vlues:
            item_dict[v[0]] = item.label
    # add a new field
    arcpy.management.AddField(layer, "PW_Zoning_Designation", "TEXT")
    arcpy.management.AddField(layer, "ColorRaw", "TEXT")
    arcpy.management.AddField(layer, "ColorHex", "TEXT")
    
    # insert the label and color values
    with arcpy.da.UpdateCursor(layer, ["PW_Zoning_Designation", "ColorRaw", "ColorHex"] + fields) as cursor:
        for row in cursor:
            # get the item
            key = tuple([str(x) for x in row[3:]])  # item.values stores values as string
            try:
                item = item_dict[key]
            # no item found -&amp;gt; default value and skip to the next feature
            except KeyError:
                row[0] = "No  Symbol"
                cursor.updateRow(row)
                continue
            # store the item's label
            row[0] = item.label
            # get the symbol color
            color = item.symbol.color
            color_type = list(color.keys())[0]
            # if it's rgb, convert to hex
            if color_type == "RGB":
                rgb = color["RGB"][:3]
                row[2] = '#%02x%02x%02x' % tuple(rgb)
            # if it's something else (hsv, hsl, cmyk), you have to convert that in other ways... here I just don't convert
            else:
                row[2] = None
            # store the color value as string for documentation
            row[1] = str(color)
            # write values
            cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;the error is on line ten:&amp;nbsp;items = renderer.groups[0].items&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ChrisGiamarino_0-1706202700439.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/92819i0AC305DDF1D487D1/image-size/large?v=v2&amp;amp;px=999" role="button" title="ChrisGiamarino_0-1706202700439.png" alt="ChrisGiamarino_0-1706202700439.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jan 2024 17:11:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374483#M69720</guid>
      <dc:creator>ChrisGiamarino</dc:creator>
      <dc:date>2024-01-25T17:11:54Z</dc:date>
    </item>
    <item>
      <title>Re: extract hex codes from layer symbology using arcpy loop</title>
      <link>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374510#M69721</link>
      <description>&lt;P&gt;I'm not quite sure what you mean by combine. Do you want to also get the colors from a symbology with grouped unique values? Or do you have layers with grouped values from multiple fields? Eh, let's do both...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To extract the item dict for different combinations of (multiple fields) and (grouped values), we have to generalize a bit. Let's take a look at how these combinations are represented in Python:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# 4 layers with different Unique Value symbologies
layer_names = [
    "unique",  # just a plain UV symbology
    "unique_grouped",  # UV from a single field, but in groups
    "unique_multiple", # UV from multiple fields, ungrouped
    "unique_multiple_grouped",  # UV from multiple fields, grouped
    ]
aprx = arcpy.mp.ArcGISProject("current")

for name in layer_names:
    layer = aprx.activeMap.listLayers(name)[0]
    renderer = layer.symbology.renderer
    first_item = renderer.groups[0].items[0]
    print(f"layer:\t{layer.name}\nfields:\t{renderer.fields}\nvalues:\t{first_item.values}\n\n")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;layer:	unique
fields:	['TextField1']
values:	[['A']]


layer:	unique_grouped
fields:	['TextField1']
values:	[['A'], ['B']]


layer:	unique_multiple
fields:	['TextField1', 'IntegerField1']
values:	[['A', '12345']]


layer:	unique_multiple_grouped
fields:	['TextField1', 'IntegerField1']
values:	[['A', '12345'], ['C', '2']]&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;values is a list of lists. There is an inner list for each value in the group. In these lists, there is a (string!) value for each field.&lt;/P&gt;&lt;P&gt;We want to use these lists as dictionary keys, but that's impossible, because keys have to be immutable, and lists are mutable, so we change them to immutable tuples. Also, we have to have a separate key for each value in a group.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def get_item_dict(symbology):
    if symbology.renderer.type != "UniqueValueRenderer":
        raise ValueError("symbology has to be Unique Value!")
    item_dict = dict()
    for group in symbology.renderer.groups:
        for item in group.items:
            for value_group in item.values:
                item_dict[tuple(value_group)] = item
    return item_dict&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, let's put the color conversion into a separate function to make the code a bit cleaner...&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def convert_to_hex(color):
    color_type = list(color.keys())[0]
    if color_type == "RGB":
        rgb = color["RGB"][:3]
        return "#%02x%02x%02x" % tuple(rgb)
    else:
        return None&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And with these two functions, we can concentrate on the actual workflow:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;layer_names = ["TestPolygons"]
aprx = arcpy.mp.ArcGISProject("current")

for name in layer_names:
    # get the layer
    layer = aprx.activeMap.listLayers(name)[0]
    # create a dict {values: item}
    item_dict = get_item_dict(layer.symbology)
    # add a new field
    arcpy.management.AddField(layer, "PW_Zoning_Designation", "TEXT")
    arcpy.management.AddField(layer, "ColorRaw", "TEXT")
    arcpy.management.AddField(layer, "ColorHex", "TEXT")
    
    # insert the label and color values
    with arcpy.da.UpdateCursor(layer, ["PW_Zoning_Designation", "ColorRaw", "ColorHex"] + layer.symbology.renderer.fields) as cursor:
        for row in cursor:
            # get the item
            key = tuple([str(x) for x in row[3:]])  # item.values stores values as string
            try:
                item = item_dict[key]
            # no item found -&amp;gt; default value and skip to the next feature
            except KeyError:
                row[0] = "No  Symbol"
                cursor.updateRow(row)
                continue
            # store the item's label
            row[0] = item.label
            # store the raw color
            row[1] = str(item.symbol.color)
            # store the hex value
            row[2] = convert_to_hex(item.symbol.color)
            # write values
            cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jan 2024 17:37:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-hex-codes-from-layer-symbology-using-arcpy/m-p/1374510#M69721</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2024-01-25T17:37:38Z</dc:date>
    </item>
  </channel>
</rss>

