skip to Main Content

I want to iterate through a dictionary array like the following to only copy the ‘symbol’ and ‘product_progress’ keys and their corresponding values to new dictionary array.

  [{'coin_name': 'Bitcoin', 'coin_id': 'bitcoin', 'symbol': 'btc', 'rank': 1, 'product_progress': 93, 'team': 100, 'token_fundamentals': 100, 'github_activity': 95, 'marketing': 5, 'partnership': 5, 'uniqueness': 5, 'total_score': 96, 'exchange_name': 'Bitfinex', 'exchange_link': 'https://www.bitfinex.com/t/BTCUSD', 'website': 'https://bitcoin.org/en/', 'twitter': 'https://twitter.com/Bitcoin', 'telegram': None, 'whitepaper': 'https://bitcoin.org/en/bitcoin-paper'}, {'coin_name': 'Ethereum', 'coin_id': 'ethereum', 'symbol': 'eth', 'rank': 2, 'product_progress': 87, 'team': 98, 'token_fundamentals': 97, 'github_activity': 100, 'marketing': 5, 'partnership': 5, 'uniqueness': 5, 'total_score': 94, 'exchange_name': 'Gemini', 'exchange_link': 'https://gemini.com/', 'website': 'https://www.ethereum.org/', 'twitter': 'https://twitter.com/ethereum', 'telegram': None, 'whitepaper': 'https://ethereum.org/en/whitepaper/'}] ... 

The code I have so far is:

    # need to iterate through list of dictionaries
    for index in range(len(projectlist3)):
        for key in projectlist3[index]:
                d['symbol'] = projectlist3[index]['symbol']
                d['token_fundamentals'] = projectlist3[index]['token_fundamentals']

print(d)

It’s just saving the last entry rather than all of the entries {‘symbol’: ‘eth’, ‘token_fundamentals’: 97}

2

Answers


  1. Given your data:

    l = [{
        'coin_name': 'Bitcoin',
        'coin_id': 'bitcoin',
        'symbol': 'btc',
        'rank': 1,
        'product_progress': 93,
        'team': 100,
        'token_fundamentals': 100,
        'github_activity': 95,
        'marketing': 5,
        'partnership': 5,
        'uniqueness': 5,
        'total_score': 96,
        'exchange_name': 'Bitfinex',
        'exchange_link': 'https://www.bitfinex.com/t/BTCUSD',
        'website': 'https://bitcoin.org/en/',
        'twitter': 'https://twitter.com/Bitcoin',
        'telegram': None,
        'whitepaper': 'https://bitcoin.org/en/bitcoin-paper'
    }, {
        'coin_name': 'Ethereum',
        'coin_id': 'ethereum',
        'symbol': 'eth',
        'rank': 2,
        'product_progress': 87,
        'team': 98,
        'token_fundamentals': 97,
        'github_activity': 100,
        'marketing': 5,
        'partnership': 5,
        'uniqueness': 5,
        'total_score': 94,
        'exchange_name': 'Gemini',
        'exchange_link': 'https://gemini.com/',
        'website': 'https://www.ethereum.org/',
        'twitter': 'https://twitter.com/ethereum',
        'telegram': None,
        'whitepaper': 'https://ethereum.org/en/whitepaper/'
    }]
    

    You can use listcomp

    new_l = [{field: d[field] for field in ['symbol', 'token_fundamentals']}
             for d in l]
    

    which is better equivalent of this:

    new_l = []
    for d in l:
        new_d = {}
        for field in ['symbol', 'token_fundamentals']:
            new_d[field] = d[field]
        new_l.append(new_d)
    
    Login or Signup to reply.
  2. Judging by what your writing into d you want to save a list of objects so this would work:

    [{"symbol": i['symbol'], "token_fundamentals": i['token_fundamentals']} for i in d]
    

    Result:

    [{'symbol': 'btc', 'token_fundamentals': 100}, {'symbol': 'eth', 'token_fundamentals': 97}]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search