Variable depth of nesting
Jay O'Connor
joconnor at cybermesa.com
Wed Feb 7 09:52:26 EST 2001
On 7 Feb 2001 05:35:03 -0800, aahz at panix.com (Aahz Maruch) wrote:
>In article <g0k872soog.fsf at scumbag.ecs.soton.ac.uk>,
>Jacek Generowicz <jmg at ecs.soton.ac.uk> wrote:
>>
>>Is it somehow possible to write loops with variable nesting depths?
>
>Try recursion.
Here's an example that will recusively print the elements in a list
>>> import types
>>> def printList(aList):
for item in aList:
if type(item) == types.ListType:
printList(item)
else:
print item
>>> printList ([1,2,3])
1
2
3
>>> printList ([1,2,3, [4,5,6]])
1
2
3
4
5
6
Take care,
Jay O'Connor
joconnor at cybermesa.com
http://www.cybermesa.com/~joconnor
Python Language Discussion Forum - http://pub1.ezboard.com/fobjectorienteddevelopmentpython
"God himself plays on the bass strings first, when he tunes the soul"
More information about the Python-list
mailing list