'python JSON for loop

every time i run it it gives me only the last value of social_score, while I want to get all the social_score values in the json API and store the output to an #iterated list to calculate with the valueI

help on this JSON

url = requests.get("https://api2.lunarcrush.com/v2?data=assets&symbol=xrp&data_points=730&interval=day&change=max").json()

def get_data():
    for data in url['data']:
        result = data['social_score']
        print(result)
        
get_data()


Solution 1:[1]

Store the intermediate results in a list variable:

url = requests.get("https://api2.lunarcrush.com/v2?data=assets&symbol=xrp&data_points=730&interval=day&change=max").json()

def get_data():
    results = []
    for data in url['data']:
        result = data['social_score']
        results.append(result)
    return results
        
result = get_data()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tyson.wu