![]() |
![]() |
![]() |
My Coding >
Programming language >
Python >
Python FAQ >
Python: How to count objects in the list
Python: How to count objects in the listCounting of objects in the list is a very common task, and now I will show few approaches for this counting Traditional counting techniqueThis is valid way of counting, but can be considered as a “bad python practice”, so should be avoided.
Pythonish way for countingIn Python you can count list content in few different ways Count with SETThe main idea is to create a unique set of all keys and then count them
Count with default dictionary defaultdictThe main idea of this way is to create defaultdict with creating every new elemnt with defauult value = 0
Count with get() methodMethod get() allow to use dictionary with any key, and if this key is not present, then get() returns default value, rather than throw an error.
So, it is up to you what technique is more suitable for you to use.
|
Last 10 artitles
9 popular artitles
|
|
© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit. Privacy Policy |