Michael T Johnson
Michael T Johnson

Reputation: 689

Append a new list from a for loops returned elements

Having an issue appending gamepk to a new list to then set and remove duplicates. My first issue is just making a new list. I can worry about the set later.

import csv
import requests
import datetime
from pprint import pprint
import pendulum

start = pendulum.datetime(2016, 4, 3)
end = pendulum.datetime(2016, 10, 2)
period = pendulum.period(start, end)

for dt in period.range('days'):
    day = dt.format('DD')
    month = dt.format('MM')
    year = dt.format('YYYY')
    the_date = str(month) + "/" + str(day) + "/" + str(year)

    try:
        req = requests.get('http://gd.mlb.com/components/game/mlb/year_' + str(year) + '/month_' + str(month) + '/day_' + str(day) + '/miniscoreboard.json') # 
        get_gameIds = req.json()['data']['games']['game']

        for gameId in get_gameIds:
            gamepk = gameId['game_pk']
            new_gamepk = []
            for pk in gamepk:                
                new_gamepk.append(pk)
                print(new_gamepk)

Printing gamepk after the second to last for loop results in a list like this:

446877
452866
446911
446873
446870
446875
446872
446876
446867
446874
446879
446871

I feel like its there that I should store the gamepk results as a list.. but im not sure. What I am trying to do is then append all of those gamepks to a new list which I can then call set on and remove the duplicates. I understand the concept of append but clearly cant seem to get it to work properly. Any help and brief explanation goes a long way!

Upvotes: 1

Views: 49

Answers (1)

jedwards
jedwards

Reputation: 30210

The idea in general is to:

  • Initialize an empty collection (list, set) outside of the outermost loop
  • Once you've found the item you want, add it to the collection.

Using a list

import csv
import requests
import datetime
from pprint import pprint
import pendulum

start = pendulum.datetime(2016, 4, 3)
end = pendulum.datetime(2016, 10, 2)
period = pendulum.period(start, end)

gamepks = []
for dt in period.range('days'):
    day = dt.format('DD')
    month = dt.format('MM')
    year = dt.format('YYYY')
    the_date = str(month) + "/" + str(day) + "/" + str(year)

    try:
        req = requests.get('http://gd.mlb.com/components/game/mlb/year_' + str(year) + '/month_' + str(month) + '/day_' + str(day) + '/miniscoreboard.json') # 
        get_gameIds = req.json()['data']['games']['game']

        for gameId in get_gameIds:
            gamepk = gameId['game_pk']
            gamepks.append(gamepk)

Using a set

import csv
import requests
import datetime
from pprint import pprint
import pendulum

start = pendulum.datetime(2016, 4, 3)
end = pendulum.datetime(2016, 10, 2)
period = pendulum.period(start, end)

gamepks = set()
for dt in period.range('days'):
    day = dt.format('DD')
    month = dt.format('MM')
    year = dt.format('YYYY')
    the_date = str(month) + "/" + str(day) + "/" + str(year)

    try:
        req = requests.get('http://gd.mlb.com/components/game/mlb/year_' + str(year) + '/month_' + str(month) + '/day_' + str(day) + '/miniscoreboard.json') # 
        get_gameIds = req.json()['data']['games']['game']

        for gameId in get_gameIds:
            gamepk = gameId['game_pk']
            gamepks.add(gamepk)

It's important to understand scoping. Variables declared inside of inner scopes (e.g. inside of for loops) "disappear" once the loop exits. That may help guide you regarding where you should initialize the variables (in this case the list/set) that you want to persist after the loop ends.

Upvotes: 2

Related Questions