Python Forum
TypeError: 'Subscription' object is not iterable in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: 'Subscription' object is not iterable in Python
#1
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
Reply
#2
(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
Reply
#3
Cool,thx
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Did you know that map() accepts more than one iterable? DeaD_EyE 2 517 Apr-13-2025, 10:16 AM
Last Post: noisefloor
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 7 3,422 Mar-09-2025, 04:25 PM
Last Post: Pedroski55
Question TypeError: argument of type 'NoneType' is not iterable Tajaldeen 7 2,473 Nov-29-2024, 09:45 AM
Last Post: Tajaldeen
  I am getting this TypeError: 'TreasureMap' object is not subscriptable. makilakos 2 1,259 May-25-2024, 07:58 PM
Last Post: deanhystad
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 2,753 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 1,762 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 3,658 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 14,145 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 5,652 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable #1 isdito2001 1 1,912 Jan-21-2023, 12:43 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020