I believe this is more straightforward with a simple for loop than using recursion. The method you have chosen cannot work because it uses only a single key returned by processline to assign the value, and that cannot create a multilevel hash. The way a recursive solution would work is...

haskell,data-structures,functional-programming,unfold

If you think of a datatype as the fixpoint of a functor then you can see that your definition is the sensible generalisation of the list case. module Unfold where Here we start by definition the fixpoint of a functor f: it's a layer of f followed by some more...

python,python-2.7,iteration,unfold

What you need is a "Generator". For example, def f(x, n): for _ in range(n): yield x x = x * x l = list(f(2, 5)) print(l) # [2, 4, 16, 256, 65536] Or def f(x): while True: yield x x = x * x for v in f(2): if...