Counting objects

The collections library has many wonderful tools, including the
Counter object, which greatly simplifies the counting of objects.

Let’s look at several examples:

  • counting letters in a string
  • counting items in a list
  • counting tuples within a tuple
  • special functions focused on using the results of a count

We’ll start by looking at three simple Python variables that contain elements we want to count:

>>> mystring = 'python pythonista pythonic the pythons'
>>> mylist = [11, 22, 22, 33, 33, 33, 42]
>>> mytuples = (('first', 'alpha'),
                ('first', 'alpha'), 
                ('second', 'beta'), 
                ('third', 'gamma'))

We import the Counter object from the collections module:

from collections import Counter

Then we provide the item we want counted as an argument to the Counter class:

>>> mystring = 'python pythonista pythonic the pythons'
>>> chars = Counter(mystring)
>>> chars
Counter({' ': 4,              # NOTE: the space is a character
         'a': 1,              # The counts are not in any order
         'c': 1,
         'e': 1,
         'h': 5,
         'i': 2,
         'n': 4,
         'o': 4,
         'p': 4,
         's': 2,
         't': 6,
         'y': 4})

This works just as well with our other samples:

>>> mylist = [11, 22, 22, 33, 33, 33, 42]
>>> integers = Counter(mylist)
>>> integers
Counter({11: 1, 22: 2, 33: 3, 42: 1})
>>> mytuples = (('first', 'alpha'), 
                ('first', 'alpha'), 
                ('second', 'beta'), 
                ('third', 'gamma'))
>>> tuples = Counter(mytuples)
>>> tuples
Counter({('first', 'alpha'): 2,
         ('second', 'beta'): 1, 
         ('third', 'gamma'): 1})

We can display the most common items in a Counter:

We do so, using the .most_common() function.
By providing an argument n to the function, we can limit our results to just nelements:

>>> chars.most_common(3)
[('t', 6), ('h', 5), ('p', 4)]

Interesting, we can see the elements organized into groups, based on the actual character, number, etc.

The .elements() method creates an iterable object, so to easily see the contents, it is common to encapsulate the result in another function, such as list() or sorted().

>>> list(chars.elements())
['p', 'p', 'p', 'p', 'y', 'y', 'y', 'y', 't', 't', 't', 't', 't', 't',
 'h', 'h', 'h', 'h', 'h', 'o', 'o', 'o', 'o', 'n', 'n', 'n', 'n', ' ',
 ' ', ' ', ' ', 'i', 'i', 's', 's', 'a', 'c', 'e']

>>> sorted(chars.elements())
[' ', ' ', ' ', ' ', 'a', 'c', 'e', 'h', 'h', 'h', 'h', 'h', 'i', 'i',
 'n', 'n', 'n', 'n', 'o', 'o', 'o', 'o', 'p', 'p', 'p', 'p', 's', 's',
 't', 't', 't', 't', 't', 't', 'y', 'y', 'y', 'y']

Counters come with a variety of other methods, most of which mirror dictionary methods:

['clear', 'copy', 'elements', 'fromkeys', 'get', 'items', 'keys',
 'most_common', 'pop', 'popitem', 'setdefault', 'subtract', 'update',

Happy coding!

Things are happening at PyHawaii: more meetups.

PyHawaii, your local Python User Group in Hawaii, is initiating two efforts:

0) a weekly series of unstructured meetups (starting on July 22nd).

1) a monthly series of larger, social structured meetups (starting on July 24th).

Visit our meetup site for details on the when and where. Can’t wait to see you there!

Interested in being even more involved with the Pacific’s Premier Python User Group?

Join our community, where you can chat, share resources, ask questions and more: email us for an invite at

– Chalmer


PyHawaii – Awesome Python Meetups, coming soon!

The Dark Art of Coding is proud to be associated with the core organizers of what promises to be an awesome series of meetups here on the Island:

PyHawaii is coming soon. We are diving headfirst into the arrangement and setup of our first Python Meetup. Can’t wait.

Still hunting for the right venue. Time and date to be announced, but it is likely to be May 21st.

To stay connected, follow/like these sources:


Python Education Summit

In 2015, PyCon will be holding its third annual Python Education Summit. This year, I have the honor of Chairing the Summit. The Summit is a gathering of teachers and educators focused on bringing coding literacy, through Python, to as broad a group of audiences as possible. We invite educators from all venues to consider joining the discussion, share insights, learn new techniques and tools and generally share their passion for education. We are looking for educators from many venues: authors; schools, colleges, universities; community-based workshops; online programs; and government.


The goal of the Summit is to bring together leaders from multiple educational venues to:

  • learn more about each other’s efforts and
  • gain useful insight from each other
  • form connections that might foster future collaboration
  • identify common issues and begin discussing ways to attack them
  • create an enhanced sense of unity, purpose and community among teachers of Python, wherever they might be

It is our hope that the Summit will serve as catalyst leading into the rest of PyCon to encourage even more interaction – hallway discussions, open spaces, lightning talks, and sprints.

Anyone attending this conference will gain a broader understanding of approaches, issues, resources and constraints in teaching Python, will have the opportunity to contribute to the discussion, and will make contacts with other teachers of Python from across the community.

Schedule and Format

The Education Summit takes place on Thursday, April 9th, the day before the main tracks of PyCon begin, so that summit attendees will be able to attend all of the regular talks.

The Summit is scheduled to run all day, from 9:00 am to 4:30 pm, with breaks in the morning and afternoon and one hour for lunch. The content and agenda are being finalized and a call for proposals is currently open. We anticipate three or four invited talks, two tracks of participant-submitted talks in the afternoon, plenty of open discussion, and a round of lightning talks.

What to expect?

We encourage all attendees to engage in the dialogue. While there will be plenty of presentations and examples of teaching techniques and tools, we are anxious to hear your questions, see your ideas and experience your passion for education and Python. So please come willing to learn, to participate and most importantly, to teach.

Learning Resources: Puzzles

One of the most important resources that you can have to facilitate learning is ‘a project‘. Something to inspire you, motivate you and drive you to keep coding.

Although I LOVE books and have many of them on programming, my best learning has come when I have something to work on NOT when I simply typed in code that someone else wrote in a book (or online course or web-tutorial).

A favorite project for me is to solve puzzles using code. Here are some places to find puzzles.

Project Euler


Code Combat

Reddit Daily Programmer

If you have other puzzles sites that interest you, let us know.

– chalmer