Lists are ordered arrays of data, which are contained in square brackets, []. Lists can contain any other type of data, including other lists. Mixing of data types, such as floats, integers, strings, or even other lists, is allowed within the same list. Lists have properties, such as length and order, which can be accessed to count and retrieve. Lists have methods to be extended, reversed, sorted, and can be passed to built-in Python tools to be summed, or to get the maximum or minimum value of the list.
Data pieces within a list are separated by commas. List members are referenced by their index or position in the list, and the index always starts at zero. Indexes are passed to square brackets [] to access these members, as in the following example:
>>> alist = ['a','b','c','d']
>>> alist[0]
'a'
This preceding example shows us how to extract the first value (at index 0) from the list called alist. Once a list has been populated, the data within it is referenced by its index, which is passed to the list in square brackets. To get the second value in a list (the value at index 1), the same method is used:
>>> alist[1]
'b'
Lists, being mutable, can be changed. Data can be added or removed. To merge two lists, the extend method is used:
>>> blist = [2,5,6]
>>> alist.extend(blist)
>>> alist
['a', 'b', 'c', 'd', 2, 5, 6]
Lists are a great way to organize data, and are used all the time in ArcPy.