Select to view content in your preferred language

arcpy.mapping.ListDataFrames - why the array index [0] on the end?

1270
4
12-01-2011 11:41 PM
JT2
by
Emerging Contributor
Can someone please explain why we need to do this:

df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]


instead of just:

df = arcpy.mapping.ListDataFrames(mxd, "Layers")


If I just say
df = arcpy.mapping.ListDataFrames(mxd)
then I can loop through the df list object by doing something like
for i in df:
  print i.name


But if I want to set variable df to a known data frame (in this case "Layers") why do I need to specify the index number [0] at the end of the code line?

If anyone can explain this quirk I'd be really grateful. Thanks
Tags (2)
0 Kudos
4 Replies
AndrewChapkowski
Esri Regular Contributor
It's not a quirk, the function returns a list object.  In order to access the underlying data inside the list, you need to tell the object what information you need by letting the list know I want some piece of information in position X.

To be even more general, functions return a specific data type. 

Hope this helps.
0 Kudos
ScottOatley
Emerging Contributor
But if I want to set variable df to a known data frame (in this case "Layers") why do I need to specify the index number [0] at the end of the code line?

If anyone can explain this quirk I'd be really grateful. Thanks


You don't. Without [0], df is a list of data frames, regardless of whether you explicitly specify a data frame, e.g. "Layers." Consider if there are multiple data frames with the same name. Of course, with [0] - or generally , df is a single data frame.

"Layers" is merely a filter that removes any data frames not named "Layers."

Scott
0 Kudos
JT2
by
Emerging Contributor
OK Thank you. I understand, so it's basically Python syntax, being the difference between a list object, and an individual member of that list, indexed by the square brackets []

Presumably if I had two data frames called "Layers" then I could index them using [0] and [1]?
0 Kudos
ArkadiuszMatoszka
Frequent Contributor


Presumably if I had two data frames called "Layers" then I could index them using [0] and [1]?


Correct 🙂
0 Kudos