Select to view content in your preferred language

Performing a Join Field Between 2 Feature Class Lists Using ArcPy

866
2
Jump to solution
02-16-2023 07:50 AM
KedaravindanBhaskar
New Contributor III

Hi all, hope you are well,

I am trying to perform a join field between two lists of feature classes using a for loop. Currently I am joining each item in the list with the corresponding item in another list individually. Here is a snippet of code -

 

#Creating list of points. Splitting into groups of 1 & 2
SamplePoints1 = ["PT1_points_DirWestLines", "PT1_points_DirSouthLines", "PT1_points_DirEastLines", "PT1_points_DirNorthLines"]
SamplePoints2 = ["PT2_points_DirWestLines", "PT2_points_DirSouthLines", "PT2_points_DirEastLines", "PT2_points_DirNorthLines"]
AllPoints = ["SamplePoints1","SamplePoints2"]

#Altering to preserve info on which group the point came from
for points2 in SamplePoints2:
    arcpy.management.AlterField(points2, "Elevation", "Elevation_2", "Elevation_2", "FLOAT", 4, "NULLABLE", "DO_NOT_CLEAR")

#Joining back both point lists
###Here is where I am facing challenges
arcpy.management.JoinField("PT1_points_DirNorthLines", "OBJECTID", "PT2_points_DirNorthLines", "OBJECTID", None, "USE_FM", 'Elevation_2 "Elevation_2" true true false 4 Float 0 0,First,#,PT2_points_DirEastLines,Elevation_2,-1,-1')
arcpy.management.JoinField("PT1_points_DirEastLines", "OBJECTID", "PT2_points_DirEastLines", "OBJECTID", None, "USE_FM", 'Elevation_2 "Elevation_2" true true false 4 Float 0 0,First,#,PT2_points_DirEastLines,Elevation_2,-1,-1')
arcpy.management.JoinField("PT1_points_DirSouthLines", "OBJECTID", "PT2_points_DirSouthLines", "OBJECTID", None, "USE_FM", 'Elevation_2 "Elevation_2" true true false 4 Float 0 0,First,#,PT2_points_DirEastLines,Elevation_2,-1,-1')
arcpy.management.JoinField("PT1_points_DirWestLines", "OBJECTID", "PT2_points_DirWestLines", "OBJECTID", None, "USE_FM", 'Elevation_2 "Elevation_2" true true false 4 Float 0 0,First,#,PT2_points_DirEastLines,Elevation_2,-1,-1')

 

 Here's the Syntax for the join field tool - 

arcpy.management.JoinField(in_data, in_field, join_table, join_field, {fields}, {fm_option}, {field_mapping})

Is there a method to achieve the join field as lists or do I have to do each individually? Any help is appreciated. Please let me know if there is anything else you need to assist me. 

Regards, Kedar

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

if the lists are paired as they appear to be, you can zip and save a line or two... assuming everything works as you have posted.

SamplePoints1 = ["PT1_points_DirWestLines", "PT1_points_DirSouthLines",
                 "PT1_points_DirEastLines", "PT1_points_DirNorthLines"]
SamplePoints2 = ["PT2_points_DirWestLines", "PT2_points_DirSouthLines",
                 "PT2_points_DirEastLines", "PT2_points_DirNorthLines"]

joins = list(zip(SamplePoints1, SamplePoints2))
joins
[('PT1_points_DirWestLines', 'PT2_points_DirWestLines'),
 ('PT1_points_DirSouthLines', 'PT2_points_DirSouthLines'),
 ('PT1_points_DirEastLines', 'PT2_points_DirEastLines'),
 ('PT1_points_DirNorthLines', 'PT2_points_DirNorthLines')]

for i in joins:
    arcpy.management.JoinField(i[0], "OBJECTID", i[1], ....etc)

... sort of retired...

View solution in original post

2 Replies
DanPatterson
MVP Esteemed Contributor

if the lists are paired as they appear to be, you can zip and save a line or two... assuming everything works as you have posted.

SamplePoints1 = ["PT1_points_DirWestLines", "PT1_points_DirSouthLines",
                 "PT1_points_DirEastLines", "PT1_points_DirNorthLines"]
SamplePoints2 = ["PT2_points_DirWestLines", "PT2_points_DirSouthLines",
                 "PT2_points_DirEastLines", "PT2_points_DirNorthLines"]

joins = list(zip(SamplePoints1, SamplePoints2))
joins
[('PT1_points_DirWestLines', 'PT2_points_DirWestLines'),
 ('PT1_points_DirSouthLines', 'PT2_points_DirSouthLines'),
 ('PT1_points_DirEastLines', 'PT2_points_DirEastLines'),
 ('PT1_points_DirNorthLines', 'PT2_points_DirNorthLines')]

for i in joins:
    arcpy.management.JoinField(i[0], "OBJECTID", i[1], ....etc)

... sort of retired...
KedaravindanBhaskar
New Contributor III

Hi Dan, 
This worked perfectly. Thanks.

Just for reference here is the complete code for the code starting in line 13

for i in joins:
    arcpy.management.JoinField(i[0], "OBJECTID", i[1], "OBJECTID",None, "USE_FM", 'Elevation_2 "Elevation_2" true true false 4 Float 0 0,First,#,PT2_points_DirEastLines,Elevation_2,-1,-1')

Regards, Kedar