Alex
Alex

Reputation: 291

TypeError: count() takes exactly one argument

I'm new to Python and Django, and I modified this code from a tutorial. I'm getting TypeError: count() takes exactly one argument (0 given) when I load the page. I've been troubleshooting and googling and can't seem to figure it out. What am I doing wrong?

def report(request):
    flashcard_list = []
    for flashcard in Flashcard.objects.all():
        flashcard_dict = {}
        flashcard_dict['list_object'] = flashcard_list
        flashcard_dict['words_count'] = flashcard_list.count()
        flashcard_dict['words_known'] = flashcard_list.filter(known=Yes).count()
        flashcard_dict['percent_known'] = int(float(flashcard_dict['words_known']) /    flashcard_dict['words_count'] * 100)
        flashcard_list.append(flashcard_dict)
    return render_to_response('report.html', { 'flashcard_list': flashcard_list })  

Upvotes: 29

Views: 69537

Answers (7)

Matt Alcock
Matt Alcock

Reputation: 12901

count() finds the number of times an item occurs in a list and hence needs that item as an argument. I think your looking for the number of items in a list. For this use len() not count()

flashcard_dict['words_count'] = len(flashcard_list)
flashcard_dict['words_known'] = len(flashcard_list.filter(known=Yes))

An example of count() would be

flashcard_dict['dog_count'] =flashcard_list.count('dog')

Upvotes: 11

Daniel Roseman
Daniel Roseman

Reputation: 600041

It's not really clear what you are trying to do here. flashcard_list is an (empty) list, which you define before your loop. It doesn't make sense to call Django queryset functions like count and filter on it (there is a list method named count, but as the error says, it takes an argument, and counts the number of times that argument is found in the list).

Did you mean to use flashcard.count() instead? That still doesn't make sense, because flashcard is a single Flashcard instance, not a queryset. You'll need to explain a bit more exactly what you're hoping to do.

Edit after comment Right, so I think the issue is that you're trying to do all this inside a loop that iterates through each Flashcard, for some reason. In fact, I don't think you want the loop at all. Something like this would be better:

def report(request):
    flashcard_dict = {}
    flashcards = Flashcard.objects.all():
    flashcard_dict['list_object'] = flashcards
    flashcard_dict['words_count'] = flashcards.count()
    flashcard_dict['words_known'] = flashcards.filter(known=True).count()
    flashcard_dict['percent_known'] = int(float(flashcard_dict['words_known']) /    flashcard_dict['words_count'] * 100)
    return render_to_response('report.html', flashcard_dict)

Here you can see that you're operating on the queryset of all flashcards, rather than an empty list. And you're building up a single dictionary, not a list of dictionaries, and that dictionary itself becomes the template context - so in the template you can reference {{ words_count }} etc.

Upvotes: 7

RanRag
RanRag

Reputation: 49597

You need to give some argument to count(x).

It returns the number of times x appears in the list.

>>> mylist  = [1,2,3,2]
>>> mylist.count(2)
2
>>> mylist.count()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: count() takes exactly one argument (0 given)
>>>

It is difficult to understand what you are trying to achieve your flashcard_list list is empty. So before calling count on it you need to add something to the list.

Upvotes: 0

El Barto
El Barto

Reputation: 929

The count method of a list counts the amount of times that x appears on the list. Check out the doc here.

If you want to know the amount of items in the list you need to use len()

>>> a = ['a', 'b', 'c']
>>> print len(a)
3

Upvotes: 3

Aaron Dufour
Aaron Dufour

Reputation: 17535

The count method is not quite what you're looking for. On Python lists, list.count(x) tells you how many occurrences of x are in list. You want len(flashcard_list). The function len is a built-in function that will tell you the length of a number of Python object types.

Upvotes: 2

01100110
01100110

Reputation: 2354

alist = []
alist.append('a')
alist.count('a')
1

This would count all the 'a' in your list.

Upvotes: 0

senderle
senderle

Reputation: 151157

count requires an argument. It returns the number of instances of a particular item in a list.

>>> l = range(10) + range(10)
>>> l.count(5)
2

2 here is the number of 5s in the list. If you want the length of a list, use len.

>>> len(l)
20

Upvotes: 30

Related Questions