Python Forum

Full Version: TypeError: 'Subscription' object is not iterable in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a list like this:
lst = [(9600002, 42, 3), (9600001, 17, 3), (9600003, 11, 1),
   (9600002, 14, 5), (9600001, 17, 1), (9600003, 11, 4),
   (9600001, 17, 4), (9600001, 14, 3), (9600002, 42, 6),
   (9600002, 42, 1)]
every tuple belongs to another class called Subscription
class Subscription:
    def __init__(self, user_id, program_code, season_id):
        self._user_id = user_id
        self._program_code = program_code
        self._season_id = season_id
    def get_user_id(self):
        return self._user_id
    def get_program_code(self):
        return self._program_code
    def get_season_id(self):
        return self._season_id
    def __repr__(self):
      return '(' + str(self._user_id) + ', ' + str(self._program_code) + ', ' + str(self._season_id) + ')'
Below is what i am doing wrong. I am trying to use Counter

This is what i got: ''TypeError: 'Subscription' object is not iterable''

from collections import Counter
from subscription import Subscription

def main():
    lst = load_subscriptions()
    c = Counter()                           #this is where i got the error
    for user, program, season in lst:      #this is where i got the error
        c[(user, program)] += 1             #this is where i got the error

def load_subscriptions():
    from sys import stdin
    lst = []
    for line in stdin:
        user_id, program_code, season_id = (int(x) for x in line.split())
        if program_code != 42:
            lst.append(Subscription(user_id, program_code, season_id))
    return lst

if __name__ == "__main__":
    main()
Then how could get this problem fixed? Any help would be appreciated! Smile Smile Smile
(Apr-09-2018, 09:47 AM)hfan6206 Wrote: [ -> ]
for user, program, season in lst: 
Ok, so if you want deconstruction to work like that, you need to do a little more work with the Subscription class, so Python knows how you want it deconstructed.

The easy way, would be to just not deconstruct it:
for sub in lst:
    user = sub._user_id
Cool,thx