How do I exclude a dataset from the ListDatasets collection

876
7
11-13-2012 05:00 AM
JoshCalhoun1
New Contributor II
Hi Everyone,

I would like to run a gp tool on a list of data sets in sde. I would however, like to exclude one data set called countours from the process if possible. Does anyone here know how that might be done? Any help you all can give me will be greatly appreciated. Below is an example of the code I am using.

thanks,

Josh Calhoun
City of Chattanooga, GIS


import arcpy
from arcpy import env
import time
import os

arcpy.env.workspace = (r'Database Connections\GADMIN.sde')

fgdb = (r'E:\ChattGIS.gdb')

arcpy.Delete_management(fgdb)

arcpy.CreateFileGDB_management(r'E:', "ChattGIS.gdb")

for ds in arcpy.ListDatasets("*","Feature"):
    print ds
    outds = fgdb +"\\"+ ds[16:]
    arcpy.Copy_management(ds, outds)
outparcelLocator = fgdb +"\\"+ "Parcels"
outstreetLocator = fgdb +"\\"+ "Streets"
arcpy.Copy_management ('Database Connections\GADMIN.sde\GADMIN.Parcels', outparcelLocator)
arcpy.Copy_management ('Database Connections\GADMIN.sde\GADMIN.Streets', outparcelLocator)
Tags (2)
0 Kudos
7 Replies
RaphaelR
Occasional Contributor II
you could try this:
...

dsets = arcpy.ListDatasets("*","Feature")

for dataset in dsets:
    if "contour" in dataset:
        dsets.remove(dataset)

for ds in dsets:
    print ds
...


or a list comprehension:
dsets = [ds for ds in arcpy.ListDatasets("*","Feature") if "contour" not in ds]
0 Kudos
JoshCalhoun1
New Contributor II
Thanks for the suggestion but I was not able to get that to work. Instead of looping through each dataset I have it hardcoded for each data set. Unfortunately, I have run out of time for more troubleshooting. There are other projects I have to get started with.

Again Thanks for your help.
0 Kudos
curtvprice
MVP Esteemed Contributor
Unfortunately, I believe there were bugs in Rafael's code. These should work:

dsets = arcpy.ListDatasets("*","Feature")
if "contour" in dsets:
  dsets.remove("contour")


The list comprehension version is kind of tricky to read, but it works too:

dsets = [ds for ds in arcpy.ListDatasets("*","Feature") if ds != "contour"]
0 Kudos
RhettZufelt
MVP Frequent Contributor
you could try this:
...

dsets = arcpy.ListDatasets("*","Feature")
rsets = []

for dataset in dsets:
    if dataset != "contour":
        rsets.append(dataset)

for ds in rsets:
    print ds
...




Often trying to remove stuff from a set/list confuses things/orders.  I would probably just make a new list exculding what you don't want.

R_
0 Kudos
RaphaelR
Occasional Contributor II
curious as to what´s bugged/why my code doesn´t work for you.
here´s what i get when i try in arcmap python window, seems to do ok:
>>> arcpy.env.workspace=("e:/test")
>>> ld = arcpy.ListDatasets("*","Raster")
>>> print ld
[u'myrasout.asc', u'ras_test2', u'ras_test22.asc', u'ras_test22.tif', u'ras_test2_2', u'ras_test3', u'ras_test3_3']

>>> for l in ld:
...     if "myrasout" in l:  # the one i´d like to filter out
...         ld.remove(l)
... 
>>> print ld
[u'ras_test2', u'ras_test22.asc', u'ras_test22.tif', u'ras_test2_2', u'ras_test3', u'ras_test3_3']


and
ld1 = [ds for ds in arcpy.ListDatasets("*","Raster") if "myrasout" not in ds]
>>> print ld1
[u'ras_test2', u'ras_test22.asc', u'ras_test22.tif', u'ras_test2_2', u'ras_test3', u'ras_test3_3']
0 Kudos
curtvprice
MVP Esteemed Contributor
curious as to what´s bugged/why my code doesn´t work for you.


Rafael, your code has two problems I should have pointed out directly. These are both things that have tripped me up so I think they are worth sharing with the list.

1. You used of the "in" operator instead of "==" to compare strings. This could give you unexpected results because
"ras_test2" in l

would give a true for elements "ras_test2","ras_test22.asc", and "ras_test2_2".

2. This one has bit me before, and I missed it again in the code above. You should not modify a list while you are looping over it. Rhett probably has run into it, which is why he wisely makes a new list. (Note this is not an issue with the list comprehension because it is creating a new list, not modifying an existing one.)

An easy way to avoid the problem is to make a copy using list():
for l in list(ld):
    if l == "contour": ld.remove(l)
0 Kudos
RaphaelR
Occasional Contributor II
What threw me of is your use of the "in" operator to compare strings. Your code would remove "contours","contour1", and "xcontour" because you are using "in" instead of "==" in your remove test


that´s true, sloppy on my part.
0 Kudos