Search code examples
pythoncombinationspython-itertools

How to handle 'NoneType' object is not iterable


I'm facing this issue on my code that was working until a couple of days ago but from today is stopped working. The issue is related to the combinations task from itertools as follow:

from itertools import combinations


def combinations(items):
    result = []
    for i in range(1, len(items) + 1):
        result.extend(itertools.combinations(items, i)) 
    page_text = page_text.css('.rcl-ParticipantFixtureDetails_LhsContainerInner')

    for match in page_text:
        teams = match.css('.rcl-ParticipantFixtureDetailsTeam_TeamName ::text').getall()
        times = match.css('.rcl-ParticipantFixtureDetails_BookCloses ::text').getall()
        row = {
            "Home": teams[0], 
            "Away": teams[1],
            "Time": times
        }
        rows.append(row)

df = pd.DataFrame(rows)
data = []
for i, (home, away, time) in enumerate(df[['Home', 'Away', 'Time']].values):
    home_words, away_words = home.split(), away.split()
    for home_combo in combinations(home_words):
        for away_combo in combinations(away_words):
            if home_combo and away_combo:
                data.append([i, home, away, time, f'{" ".join(home_combo)} - {" ".join(away_combo)}'])

this is the error

     97 for i, (home, away, time) in enumerate(df[['Home', 'Away', 'Time']].values):
     98     home_words, away_words = home.split(), away.split()
---> 99     for home_combo in combinations(home_words):
    100         for away_combo in combinations(away_words):
    101             if home_combo and away_combo:

TypeError: 'NoneType' object is not iterable

Solution

  • The problem is here:

    from itertools import combinations
    
    def combinations(items):
        ...
    

    Your combinations function masks the combinations you import from itertools, so you're just calling your own function, which implicitly returns None. Just rename your function to something else and you'll be able to reference the desired imported function.