# Combining muliple values into one cell (devided by comma, based on different attribute)

410
1
07-28-2021 07:39 AM
New Contributor III

Hi,

is there a simple way in ArcGis Pro of combining multiple attribute1 values (with comma as a delimiter) into one cell based on attribute2 values. Here is a simple example:

As a result, I would like to get a table with two lines

attr2 | attr1

1 | "1"

2 | "2, 5"

Thank you for any help. I believe there is some very simple solution, but I did not find it 😕

Best from

T.

Tags (1)
1 Solution

Accepted Solutions
New Contributor III

this could be the solution (I believe it can be way shorter)

outDict = {}
columns = ['treeNumber', 'parcel']
curObj = arcpy.da.SearchCursor("tmpIntersect", columns) # tmpIntersect is original table
for row in curObj:
if row[0] in outDict:
outDict[row[0]].append(row[1])
else:
outDict[row[0]] = [row[1]]

arcpy.CreateTable_management(gdb, outputT)

rows = arcpy.InsertCursor(outputT, ["itemNumber","parcel"])
for x, y in outDict.items():
row = rows.newRow()
row.setValue("itemNumber",x)
row.setValue("parcel", ", ".join(y))
rows.insertRow(row)

arcpy.Delete_management(gdb+"\\tmpIntersect")

New Contributor III

this could be the solution (I believe it can be way shorter)

outDict = {}
columns = ['treeNumber', 'parcel']
curObj = arcpy.da.SearchCursor("tmpIntersect", columns) # tmpIntersect is original table
for row in curObj:
if row[0] in outDict:
outDict[row[0]].append(row[1])
else:
outDict[row[0]] = [row[1]]

arcpy.CreateTable_management(gdb, outputT)