def flatten_txns_by_hash(tx_list, nesting=True):
    '''
    Flattens a response from querying a list of address (or wallet) transactions

    If nesting==True then it will return an ordered dictionary where the keys are tranasaction hashes, otherwise it will be a list of dicts.
    (nesting==False is good for django templates)
    '''
    nested_cleaned_txs = OrderedDict()
    for tx in tx_list:
        tx_hash = tx.get('tx_hash')

        satoshis = tx.get('value', 0)  # rare edge case where API returns 0

        if tx.get('tx_input_n') >= 0:
            satoshis *= -1

        if tx_hash in nested_cleaned_txs:
            nested_cleaned_txs[tx_hash]['txns_satoshis_list'].append(satoshis)
            nested_cleaned_txs[tx_hash]['satoshis_net'] = sum(nested_cleaned_txs[tx_hash]['txns_satoshis_list'])
            if tx.get('double_spend') and not nested_cleaned_txs[tx_hash]['double_spend']:
                nested_cleaned_txs[tx_hash]['double_spend'] = True

        else:
            nested_cleaned_txs[tx_hash] = {
                    'txns_satoshis_list': [satoshis, ],
                    'satoshis_net': satoshis,
                    'received_at': tx.get('received'),
                    'confirmed_at': tx.get('confirmed'),
                    'confirmations': tx.get('confirmations', 0),
                    'block_height': tx.get('block_height'),
                    'double_spend': tx.get('double_spend', False),
                    }
    if nesting:
        return nested_cleaned_txs
    else:
        unnested_cleaned_txs = []
        for tx_hash in nested_cleaned_txs:
            tx_cleaned = nested_cleaned_txs[tx_hash]
            tx_cleaned['tx_hash'] = tx_hash
            unnested_cleaned_txs.append(tx_cleaned)
        return unnested_cleaned_txs
Flattens a response from querying a list of address (or wallet) transactions

    If nesting==True then it will return an ordered dictionary where the keys are tranasaction hashes, otherwise it will be a list of dicts.
    (nesting==False is good for django templates)