↩ back to the box
1, 2, 3, ...

Watch This Lesson As A Video
( https://t.dripemail2.com/c/eyJhY2NvdW50X2lkIjoiMzg3MzQxNiIsImRlbGl2ZXJ5X2lkIjoiNGs0ZXBnMTR5djA3dWUxNWtrYWgiLCJ1cmwiOiJodHRwczovL2Jsb2cuZmlueHRlci5jb20vcHl0aG9uLWxpc3QtY291bnQvP3RsX2luYm91bmQ9MVx1MDAyNnRsX3RhcmdldF9hbGw9MVx1MDAyNnRsX2Zvcm1fdHlwZT0xXHUwMDI2dGxfcGVyaW9kX3R5cGU9M1x1MDAyNl9fcz1oN2cxNmxqOGpiNGhweTVkajczZyJ9 )

By studying the content in this email, you'll not only understand
the count() method of Python lists, but you'll also become a
better computer scientist.

Specification: The list.count(x) method counts the number of
occurrences of the element x in the list.

Here’s a short example:
>>> lst = [1, 2, 42, 2, 1, 42, 42] >>> lst.count(42) 3 >>>
lst.count(2) 2
In the first line of the example, you create the list lst. You
then count the number of times the integer values 42 and 2 appear
in the list.

Return value: The method list.count(value) returns an integer
value set to the number of times the argument value appears in
the list. If the value does not appear in the list, the return
value is 0.

It’s important that you understand how the count() method works.
Say, you’re looking for the value in a given list. If list
element x == value, the counter is increased by one.

Here’s an example:

>>> lst = [[1,1], [1,1], (1,1), 42, 1] >>> lst.count([1,1]) 2
The top-level list refers to two independent list objects [1, 1]
in memory. Still, if you count the number of occurrences of a
third list [1, 1], the method correctly determines that it
appears two times. That’s because two list elements are equal to
the list [1, 1]:

lst[0] —> list [1,1]
lst[1] —> list [1,1]
lst[2] —> tuple (1,1)

Note that values x and y are considered equal if x==y. You can
see this for integer lists in the following example:

>>> lst_1 = [1, 1] >>> lst_2 = [1, 1] >>> lst_1 == lst_2 True
To summarize, the list.count(value) method counts the number of
times a list element is equal to value (using the == comparison).
Here’s a reference implementation:

def count(lst, value): ''' Returns the number of times a list
element is equal to value''' count = 0 for element in lst: count
+= element == value return count lst = [1, 1, 1, 1, 2]
print(lst.count(1)) # 4 print(lst.count(2)) # 1
print(lst.count(3)) # 0
(Note that this is not the real cPython implementation. It’s just
a semantically equivalent implementation of the list.count(value)
method for educational purposes.)

(Worst-Case) Time complexity: the standard Python implementation
cPython “touches” all elements in the original list to check if
they are equal to the value. Thus, the time complexity of the
count() method is linear in the number of list elements:

* For one thousand elements, you'll have to perform one thousand
* For two thousand elements, you'll have to perform two thousand
comparisons and so on.

(For pros: we say that the runtime complexity is O(n) for a list
with n elements.)

The figure shows how the elapsed time of counting a dummy element
-99 in lists with a growing number of elements grows linear to
the number of elements.

Never stop learning! :D

PS: If you haven't already, check out my brand-new Python book
with the publisher NoStarch Press in San Francisco.

If you enjoyed any of the content in this email series, you'll
love the book! We've spent hundreds of hours crafting the book --
discussing every sentence in it until we finally reached a stable

This is the best book I've ever written for sure:

New Book: Python One-Liners Superpower
( https://t.dripemail2.com/c/eyJhY2NvdW50X2lkIjoiMzg3MzQxNiIsImRlbGl2ZXJ5X2lkIjoiNGs0ZXBnMTR5djA3dWUxNWtrYWgiLCJ1cmwiOiJodHRwczovL3d3dy5hbWF6b24uY29tL2dwL3Byb2R1Y3QvQjA3Wlk3WE1YOD9fX3M9aDdnMTZsajhqYjRocHk1ZGo3M2cifQ )(The button leads
to the Amazon Book page.)

How are we doing? ( https://t.dripemail2.com/c/eyJhY2NvdW50X2lkIjoiMzg3MzQxNiIsImRlbGl2ZXJ5X2lkIjoiNGs0ZXBnMTR5djA3dWUxNWtrYWgiLCJ1cmwiOiJodHRwczovL2Zvcm1zLmdsZS9pZnN5azlZa1o0azVBOWZqNz9fX3M9aDdnMTZsajhqYjRocHk1ZGo3M2cifQ )
⭐ ( https://t.dripemail2.com/c/eyJhY2NvdW50X2lkIjoiMzg3MzQxNiIsImRlbGl2ZXJ5X2lkIjoiNGs0ZXBnMTR5djA3dWUxNWtrYWgiLCJ1cmwiOiJodHRwczovL2Zvcm1zLmdsZS9qUDE3aml4QmhFNEJ6NlZxOD9fX3M9aDdnMTZsajhqYjRocHk1ZGo3M2cifQ )
⭐⭐ ( https://t.dripemail2.com/c/eyJhY2NvdW50X2lkIjoiMzg3MzQxNiIsImRlbGl2ZXJ5X2lkIjoiNGs0ZXBnMTR5djA3dWUxNWtrYWgiLCJ1cmwiOiJodHRwczovL2Zvcm1zLmdsZS9qUDE3aml4QmhFNEJ6NlZxOD9fX3M9aDdnMTZsajhqYjRocHk1ZGo3M2cifQ )
⭐⭐⭐ ( https://t.dripemail2.com/c/eyJhY2NvdW50X2lkIjoiMzg3MzQxNiIsImRlbGl2ZXJ5X2lkIjoiNGs0ZXBnMTR5djA3dWUxNWtrYWgiLCJ1cmwiOiJodHRwczovL2Zvcm1zLmdsZS9qUDE3aml4QmhFNEJ6NlZxOD9fX3M9aDdnMTZsajhqYjRocHk1ZGo3M2cifQ )

To make sure you keep getting these emails, please
add chris@finxter.com to your address book or whitelist us.

I'd love to hear your feedback so that I can improve this free
email course over time. Please reply to this email and share
everything that's on your mind!

If you find the Finxter Email Academy useful, please invite a
friend or colleague! ❤
Here's the subscription link you can share:

Download the Ultimate Python Cheat Sheet here (direct PDF
download): 🐍
The Ultimate Python Cheat Sheet
( https://t.dripemail2.com/c/eyJhY2NvdW50X2lkIjoiMzg3MzQxNiIsImRlbGl2ZXJ5X2lkIjoiNGs0ZXBnMTR5djA3dWUxNWtrYWgiLCJ1cmwiOiJodHRwczovL2Jsb2cuZmlueHRlci5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMjAvMDcvRmlueHRlcl9Xb3JsZHNNb3N0RGVuc2VQeXRob25DaGVhdFNoZWV0LnBkZj9fX3M9aDdnMTZsajhqYjRocHk1ZGo3M2cifQ )

Want out of the loop? https://t.dripemail2.com/c/eyJhY2NvdW50X2lkIjoiMzg3MzQxNiIsImRlbGl2ZXJ5X2lkIjoiNGs0ZXBnMTR5djA3dWUxNWtrYWgiLCJ1cmwiOiJodHRwczovL3d3dy5nZXRkcmlwLmNvbS9zdWJzY3JpYmVycy9oN2cxNmxqOGpiNGhweTVkajczZy9zdWJzY3JpcHRpb25zP2Q9NGs0ZXBnMTR5djA3dWUxNWtrYWhcdTAwMjZleGNsdWRlX2NsaWNrPTFcdTAwMjZpbnRlbnQ9dW5zdWJzY3JpYmVcdTAwMjZfX3M9aDdnMTZsajhqYjRocHk1ZGo3M2cifQ. I'm so sad to see you go. 😢
How could we have done better? To help future Finxters, please
hit reply and tell us! 🤗

Not very motivated to learn today? Consider this:
"Knowledge compounds!" -- Warren Buffett

Consequently, if you improve your skills by 1% every day, you'd
36x your programming skills within a year!

Schurwaldstrasse 61, 70186 Stuttgart

Warning: the message above can be a phishing scam. See: legal notes