[Django]-TypeError: count() takes exactly one argument

28👍

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

11👍

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')

7👍

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.

3👍

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

2👍

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.

0👍

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.

👤RanRag

0👍

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

This would count all the ‘a’ in your list.

Leave a comment