In the following code (line 3), I dont understand why split('.')[-1] is used and what it does.

arcpy.env.workspace = sde_gdb

for table in arcpy.ListTables():

    t_name = table.split('.')[-1]
    arcpy.TableToGeodatabase_conversion(table, gdb)
  • 1
    This is a question about Python and really doesn't have anything to do with GIS. Specifically, you are asking about the split function and list indexing. – Evil Genius Apr 25 '17 at 19:45

The python built in function split() will break apart a string based on the character parameter passed to it and return a list of strings. Lets say your "table" variable has the following value:

table = "sde.tblExample"

running table.split('.') will split the string into a list of two parts: ['sde','tblExample'] because you specified to split on the '.' character

If the table value was "dbo.sde.tblExample" table.split('.') would result in a list of 3 string values: ['dbo','sde',tblExample']

In python you use brackets [] to specify a position within a list. -1 means the last position, and 0 means the first position in a list.

So, running

table = "sde.tblExample"
t_name = table.split('.')[-1] 

will return the list item in the -1 position (or, last position) from the list and pass it to the variable t_name, giving you the result 'tblExample'


t_name = table.split('.') will take the content of table and break it at each . and will save it in the list t_name
ex: public.myTable ==> the list will contain two items, public and myTable

The list index [-1] means the last item of the list. Negative indexes counts from the end.

Using the example public.myTable, the output t_name will be myTable.

Not the answer you're looking for? Browse other questions tagged or ask your own question.