1

I'm learning to know how Python Dictionaries work and is writing a small program to know if the word awesome in the word count column, then it should return me the value.

However, it is giving me the following error:

TypeError: argument of type 'type' is not iterable

My word_count column is like below:

{'and': 5, 'stink': 1, 'because': 2}
{'awesome': 3, 'bad': 2}
{'mac': 5, 'awesome': 1}

I have created this function:

def awesome_count(self):
    if 'awesome' in dict:
        return dict['awesome']
    else:
        return 0

after then, I'm using apply function to get the count, but getting an error:

products['awesome'] = products['word_count'].apply(awesome_count)

Expecting the following answer:

0,3,1 i.e number of times awesome is present 
10
  • if 'awesome' in dict: return dict['awesome'] Where is dict defined? Commented Oct 31, 2015 at 11:21
  • do I need to pass dict as a parameter to the function?. and to answer your question, dict is not defined anywhere. Commented Oct 31, 2015 at 11:23
  • 1
    It needs to be defined. Looks like this function is part of a class and acts on the object itself? Then dict should be the member of your class that you want to look at. Commented Oct 31, 2015 at 11:24
  • Your code makes no sense.. what is that self parameter for? How is dict defined? What exactly are you trying to do. Commented Oct 31, 2015 at 11:38
  • @user4943236 If dict is not defined anywhere, where will the key:value pairs come from? Also consider reading a book Commented Oct 31, 2015 at 11:39

2 Answers 2

1

I am not sure if I got your idea, but I noted two things:

  1. You must indent the body of your function to mark the beginning and end of it. Python should give you an error if you type the way you did here.

  2. you called an apply method from the value products['soemthing'] that seems to me to be an integer value so it does not have a 'apply' method. The TypeError you receive tells you to call apply in an iterable object such as a list not on an integer.

Suggestion: If you just want to write a function that returns the value associated with awesome in the dictionary write

def awesome_count(dict):
    if 'awesome' in dict:
        return dict['awesome']
    return 0

Then call this function directly to your dictionaries.

Sign up to request clarification or add additional context in comments.

1 Comment

"dict" is a reserved keyword in Python and shouldn't be used for a parameter name
1

if products['word_count'] is a list of dict, try this:

products = {}
products['word_count'] = [{'and': 5,'stink': 1, 'because': 2}, 
                          {'awesome': 3, 'bad': 2}, 
                          {'mac': 5, 'awesome': 1}]

products['awesome'] = [d.get('awesome',0) for d in products['word_count']]

1 Comment

Thanks for your answer. May I know if I change the function definition from def awesome_count(self): to def awesome_count(dict): how does it impact the function? can you help me clearing this concept

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.