-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnested_for_loop_without_nesting_advanced.py
More file actions
62 lines (43 loc) · 1.07 KB
/
nested_for_loop_without_nesting_advanced.py
File metadata and controls
62 lines (43 loc) · 1.07 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''
Example of how to nest a for loop, but by using a function
in place of the nested for loop.
This has the result of keeping the level of indentation lower.
The result is a list of dicts which looks like this:
[{},
{0: 1},
{0: 2, 1: 2},
{0: 3, 1: 3, 2: 3},
{0: 4, 1: 4, 2: 4, 3: 4},
{0: 5, 1: 5, 2: 5, 3: 5, 4: 5},
{0: 6, 1: 6, 2: 6, 3: 6, 4: 6, 5: 6},
{0: 7, 1: 7, 2: 7, 3: 7, 4: 7, 5: 7, 6: 7},
{0: 8, 1: 8, 2: 8, 3: 8, 4: 8, 5: 8, 6: 8, 7: 8},
{0: 9, 1: 9, 2: 9, 3: 9, 4: 9, 5: 9, 6: 9, 7: 9, 8: 9}]
Can this be extended to 3 levels or even n-levels ?
'''
__author__ = 'dlefcoe'
# imports
import pprint
def main():
''' main entry point for the code '''
res = outer_for()
pprint.pp(res)
print('---')
res = inner_for(9)
pprint.pp(res)
return
def outer_for():
''' the outer for loop '''
ans = []
for i in range(10):
res = inner_for(i)
ans.append(res)
return ans
def inner_for(i):
d = {}
for j in range(i):
d[j] = i
return d
# main guard idiom
if __name__=='__main__':
main()