Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
edited by

For loop is only outputs the marketids, for the last set of races of  JSON file. I think this is because of the if statement preceding the for a loop. Does anyone know how can I continue looping whilst keeping the if statement?

My code:

request1 = requests.get('https://betfair-data-supplier-prod.herokuapp.com/api/daily_racing_results?date=2020-01-02')

json1 = request1.json()

marketids = []

for market in json1:

    if market['raceType'] in ['R']:

        marketids.append(market["markets"][0]["marketId"])

print(marketids)

The output of code:

['166897895', '166892549', '166896013', '166892651', '166892683', '166897912']

Example of json1:

[{'venueName': 'Geelong',

  'raceDay': '2020-01-02 00:00:00',

  'country': 'AUS',

  'raceType': 'R',

  'meetingId': '29637049',

  'markets': [{'marketId': '166897895',

    'raceNo': '1',

    'startTime': '2020-01-02 02:00:00',

    'marketStatus': 'CLOSED',

    'eventName': 'Port Water Bouy Mdn Plate',

    'nzToteEventId': '287012'},

   {'marketId': '166897897',

    'raceNo': '2',

    'startTime': '2020-01-02 02:30:00',

    'marketStatus': 'CLOSED',

    'eventName': 'Portarlington Beach Motel Mdn',

    'nzToteEventId': '287013'},

   {'marketId': '166897899',

    'raceNo': '3',

    'startTime': '2020-01-02 03:00:00',

    'marketStatus': 'CLOSED',

    'eventName': 'Flash Fabrications Mdn Plat3',

    'nzToteEventId': '287014'},

   {'marketId': '166897901',

    'raceNo': '4',

    'startTime': '2020-01-02 03:30:00',

    'marketStatus': 'CLOSED',

    'eventName': 'Race With Empower Racing Mdn',

    'nzToteEventId': '287015'},

   {'marketId': '166897903',

    'raceNo': '5',

    'startTime': '2020-01-02 04:00:00',

    'marketStatus': 'CLOSED',

    'eventName': 'Absolute Landscapes Mdn Plate',

    'nzToteEventId': '287016'},

   {'marketId': '166897905',

    'raceNo': '6',

    'startTime': '2020-01-02 04:30:00',

    'marketStatus': 'CLOSED',

    'eventName': 'Ajk Builders (Bm64)',

    'nzToteEventId': '287017'},

   {'marketId': '166933411',

    'raceNo': '7',

    'startTime': '2020-01-02 05:00:00',

    'marketStatus': 'CLOSED',

    'eventName': 'Batman Mgmt Bellarine Cup-Bm64',

    'nzToteEventId': '287018'},

   {'marketId': '166897909',

    'raceNo': '8',

    'startTime': '2020-01-02 05:30:00',

    'marketStatus': 'CLOSED',

    'eventName': 'Gj Bradding Heating&Cool.-Bm58',

    'nzToteEventId': '287019'},

   {'marketId': '166897911',

    'raceNo': '9',

    'startTime': '2020-01-02 06:00:00',

    'marketStatus': 'CLOSED',

    'eventName': 'Bayshore Electrical (Bm58)',

    'nzToteEventId': '287020'}]},

 {'venueName': 'Mount Barker',

  'raceDay': '2020-01-02 00:00:00',

  'country': 'AUS',

  'raceType': 'R',

  'meetingId': '29636945',

  'markets': [{'marketId': '166892549',

    'raceNo': '1',

    'startTime': '2020-01-02 05:17:00',

    'marketStatus': 'CLOSED',

    'eventName': 'Happy New Year Mdn',

    'nzToteEventId': ''},

   {'marketId': '166892551',

    'raceNo': '2',

    'startTime': '2020-01-02 05:52:00',

    'marketStatus': 'CLOSED',

    'eventName': 'Langton Mdn',

    'nzToteEventId': ''},

   {'marketId': '166892553',

    'raceNo': '3',

    'startTime': '2020-01-02 06:32:00',

    'marketStatus': 'CLOSED',

    'eventName': 'McDonald Hcp (C2)',

    'nzToteEventId': ''},

   {'marketId': '166892555',

    'raceNo': '4',

    'startTime': '2020-01-02 07:10:00',

    'marketStatus': 'CLOSED',

    'eventName': 'Hicks Hcp (C2)',

    'nzToteEventId': ''},

   {'marketId': '166892557',

    'raceNo': '5',

    'startTime': '2020-01-02 07:45:00',

    'marketStatus': 'CLOSED',

    'eventName': 'Lowood (Bm58+)',

    'nzToteEventId': ''}]}]

1 Answer

0 votes
by (36.8k points)
edited by

Use the below code: 

request1 = requests.get('https://betfair-data-supplier-prod.herokuapp.com/api/daily_racing_results?date=2020-01-02')

json1 = request1.json()

marketids = []

for market in json1:

    if market['raceType'] in ['R']:

        for id in market['markets']:

            marketids.append(market["markets"][id]["marketId"])

print(marketids)

edit solution by @Sushanth

[i['marketId'] for v in request1.json() if v['raceType'] == 'R' for i in v['markets']]

Learn Data Science with Python Course to improve your technical knowledge.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...