-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnested.py
More file actions
30 lines (24 loc) · 761 Bytes
/
nested.py
File metadata and controls
30 lines (24 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""
print nested data structure with increasing indentation at each level
[ 'I....', 'II...', [ 'A...', 'B...' ], 'III....' ]
is printed
I...
II...
A...
B...
III...
"""
outline = [ 'I....', 'II...', [ 'A...', [ 1, 2 ], 'B...' ], 'III....' ]
def print_nested(spaces, data):
"""
print nested data with increasing indentation at each level
data is list of non-lists (strings, numbers...) and/or nested lists
spaces is indentation, typically '' (empty string) at top level
"""
if isinstance(data, list):
for item in data:
print_nested(spaces + ' ', item) # recursive calls
else:
print '%s%s' % (spaces, data) # base case
if __name__ == '__main__':
print_nested('', outline)