Defining a list in Python is a simple matter of assigning a number of Python objects to a variable name using the = operator. The list needs to be enclosed in square brackets and can include any makeup of Python objects. A simple numeric list acts much like an array; however, lists are much more dynamic and can include many different types within the same list. The code example in def_list.py demonstrates the creation of both homogeneous and heterogeneous lists. Notice in the example that the lists include numbers, strings, list definitions, and variable names. numList = [2000, 2003, 2005, 2006] stringList = ["Essential", "Python", "Code"] mixedList = [1, 2, "three", 4] subList = ["Python", "Phrasebook", \ ["Copyright", 2006]] listList = [numList, stringList, mixedList, subList] for x in listList: for y in x: if isinstance(y, int): print y + 1 if isinstance(y, basestring): print "String:" + y def_list.py 2001 2004 2006 2007 String: Essential String: Python String: Code 2 3 String: three 5 String: Python String: Phrasebook Output from def_list.py code |