« Back to Index

Python 3: Reduce Array/List into Dict

View original Gist on GitHub

Python 3: Reduce Array List into Dict.py

def fn():
    count = 0
    
    def inner(acc, val):
        nonlocal count 
        count += 1
        acc[count] = val
        
        return acc
         
    return inner
    
reduce(fn(), ['a', 'b', 'c', 'd'], {})

# {1: 'a', 2: 'b', 3: 'c', 4: 'd'}