FAQ: Learn Python - Practice Makes Perfect - count

This community-built FAQ covers the “count” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise count:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

1 Like

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!
1 Like

2 posts were split to a new topic: What is the list method that you can use for this?

I don’t understand this code at all, what does that sequence link to?

i did it in idle and it is working. what is the problem with cc?

a = [1, 2, 1, 1]
b = 1

if b in a:

It runs fine for me in the cc interface. What is the problem, precisely? And why do you cast to string?


I did it on 3.7 and it does not allow me to count int, so i had to string em. When tryed it on cc it gave wrong output using return :confused:

We are meant to write our own count function, not use the built in method.

def count(sequence, item):
    c = 0
    for s in sequence:
        c += 1 if s == item else 0
    return c

or something along those lines.

Bottom line, don’t use the str.count() method since this function is intended to accept any type of sequence, numbers, strings, even lists.

print (count([[1,2], [1, 2], [1, 2]], [1, 2]))
# 3

What do you mean by working and not working though? I’m not convinced that the same measuring stick is used in both cases.
And, is 11 two ones? Are 11 trees the same as one tree and one tree? That’s going from one count to multiple counts, it’s not the same amount and it’s not the same shape.

1 Like

Even if u try to print str(a) it’ll print whole list, not stringed numbers. So when it goes thru list it’s counting same symbols, and returns number of simbols. It does not matter they are string int or float. So when i tried it during lesson it gave completly different result instead of what i was seeng in my idle, that’s why i asked the question. U can try coping it and using ot and u’ll see how it works.

If you print a list then you are DEFINITELY seeing a string. It’s all a string, or you wouldn’t be able to print it.
Maybe strings shouldn’t be involved at all, this is not a problem about text, right.

1 Like

ok, lets see…
i did it under def as lessons asked. just used that example to see what it gave and how it worked.
this thing here returns correct answer right? without some extra writing…

def count(sequence, item):
    return str(sequence).count(str(item))
print(count([[1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]],2))

it’s just, for previous code, when it was getting wrong answer and i looked at input, it should have worked… i missed something probably…

oh, and when i string a list, it just strings list elements, not list itself ionatan -_-

Well, first of all, it doesn’t implement count, it uses another implementation of count.

It’s not a text problem, so what makes str helpful? Is it helpful?

Since it counts characters, not numbers, it will for example count 11 as two instances of 1, which it isn’t (you’re counting substrings in text, not numbers in a list)

No that converts the whole thing into a string, it does not create a list of string elements.

1 Like

Since it’s askint to count, what does it matters it counts symbols or numbers?
I did str coz it does not allow me to count int, and it counts number of 1-s, sso in cas of 11 it’ll be 2

What is >it<? And what does allow mean?

It wouldn’t, but you are treating it like a single sequence of characters, not a list of symbols.

'11'.count('1')  # 2

You’d also be able to find other things in there, like spaces, commas, square brackets.

1 Like

“it” in this case is my IDLE. it gives me error'int' object has no attribute 'count'

You’d also be able to find other things in there, like spaces, commas, square brackets.

yes but since lessons is asking to count specific symbol, my code works just fine.

Your function should handle any sequence, not just strings.

>>> count('mississippi', 'i')
>>> count([1,2,3,2,3,4,3,4,5,1,2,3], 3)
>>> count((1,2,3,2,3,4,3,4,5,1,2,3), 3)
1 Like

Right but that’s not a matter of being allowed, it doesn’t make sense. Maybe you meant something else that does make sense. THAT is what you should be going for. Not text.

Not if you are looking for 1 and there exists 11 123… anything that has your number as a substring

1 Like
def count(sequence, item):
    return str(sequence).count(str(item))
print(count([[1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]],2))

does that just fine

if u r talking about my first code i wrote here, it does not work for sublists. i understood that. but later one does all the counting.