That One Thing

Create a Dictionary From a List

Count the number of times an item appears in a Python list, with code examples

Jamel Dargan
The Startup

--

Photo by Lorenzo Lamonica on Unsplash

You know that road-trip game, where you only count cars you see if they are of the same color. On a long, lonely stretch of road, it is a game of anticipation. On a busy highway, it can get a bit neurotic. Sometimes, the game can morph into one of comparing how many different colored cars you can count. Without some sort of tally sheet, keeping count in this way quickly can get out of hand. Even if you make a list comprising each observation, counting them up for each category can be a bit of a hassle.

A digital list can be just as trying. You may be able to highlight or sort a spreadsheet list, to make counting a little easier, or perhaps you could aggregate fields in a SQL database. In Python, you also have a few options for managing such a task.

In this article, will review the following:

  • Understanding the Python list
  • What is a dictionary, in Python?
  • Converting list elements into dictionary keys
  • How you can count item occurrences in a list, by converting the list to a dictionary
  • Code for implementation in Python

Lists in Python

In Python, a list can be any sequence of values. We indicate a list by enclosing the values in square brackets and separating them with commas. Lists may contain numbers, text strings, or any type of object (or a mixture of objects).

A Python list of integers from 1–5 looks like this:

# spaces after the commas are not required
[1, 2, 3, 4, 5]

Strings in a list are surrounded by quotes:

# a four-item combination of strings and numbers
["cat",3,1,"dog"]

Lists can even contain other lists:

# a four-item list of two lists, a number and a string
[[1, 2, 3, 4, 5], 541, ["cat",3,1,"dog"], "Chuck"]
# and, of course, we can assign a variable to our list
my_mixed_list = [[1, 2, 3, 4, 5], 541, ["cat",3,1,"dog"], "Chuck"]

Python lists are mutable. This means you can change them. You can perform math on list items, change their order, or append new items to the list. In fact, the actions that may be performed on lists amounts to — well — a very long list. Let’s see what a couple of these operations look like.

Operation 1:

# multiplying a list to extend the list
my_list = [1, 2, 3, 4, 5]
result = my_list * 2
print(result)
> [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

The above operation results in a list that is twice as long, with each item reoccurring in its original order. We can verify the lengths or number of items in our lists with Python’s len() list method:

# printing the length of `my_list`
print("The length of my_list is ", len(my_list))
> The length of my_list is 5# printing the length of `result`
print("The length of result is ", len(result))
> The length of result is 10

Operation 2:

# multiplying list items
my_list = [1, 2, 3, 4, 5]
# creating a new list by multiplying each `mylist` item (i) by two
result2 = [i*2 for i in my_list]
print(result2)> [2, 4, 6, 8, 10]

This second operation results in a list (result2) that is the same length as our original (my_list), but each number is multiplied by two.

Note: We used len() to find the number of items, in our lists. In addition, we can use Python’s built-in range method to find start-and-stop indexes:

print(range(len(my_list)))
> range(0, 5)
print(range(len(result)))
> range(0, 10)

Python indexing begins at zero. The range for my_list starts at index 0 — the value for which is 1 in my_list — and goes up to (but does not include) index 5.

We will use range() in the code for our main task. The task is to count how many times each item occurs on our list. For example: on the five-item list [“dog”, “cat”, “dog”, “cat”, “mouse”], “dog” and “cat” each occur twice, and “mouse” occurs once. To accomplish our task in Python, we will need to identify every unique item in the list and account for any duplicates. Our method for this task will involve using a Python dictionary.

Picture of an open dictionary with a red cover on a wood surface, tilted forward at about a 45-degree angle.
Photo by Pisit Heng on Unsplash

(Python) dictionaries

A Python dictionary is a collection of data values, stored as key-value pairs. For the print dictionary pictured above, if each word were thought of as a key, then it’s definition might be its value (in a simple analogy).

Python dictionaries are written with curly braces. Below is an example of a dictionary with two entries:

# a dictionary of student heights in feet and inches
student_height = {"Johnette K.":"5'7", "Carl D.":"5'11"}

Notice that each key, here indicating a student’s first name and last initial, is followed by a colon. Each key’s matching value follows the colon. In the above example, the matching value for the key, “Carl D.”, is “5'11”. Dictionary keys are unique and immutable — there can only be one “Carl D.”; however, as with lists, dictionary values can reference any type of object, including lists, other dictionaries, or even databases (but, let’s not get ahead of ourselves).

So, how does a dictionary help us with our task?

Converting a list to a dictionary

Since dictionary keys are unique and immutable, if we convert list items to dictionary keys we will have a key for each unique item in our list. Converting the elements in our ten-item result list to dictionary keys would produce a dictionary with five unique keys:

# multiplying a list to extend the list
my_list = [1, 2, 3, 4, 5]
result = my_list * 2
print(result)
> [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]# creating an empty dictionary
converted_dict = {}
# updating `converted_dict` with keys for each element (i) in `result`
for i in result:
converted_dict.update({i:""})
print(converted_dict)
> {1: '', 2: '', 3: '', 4: '', 5: ''}

Note: We use the ‘update’ dictionary method to add or change key-value pairs (similar to how we used append() to add to a list). We created an empty string value (“”), for each key. We could also have updated None as the value for each key.

Wait!

Doesn’t this just put us back where we started?

Admittedly, our dictionary looks a lot like our list, with a colon and an empty string added to each item. However, key-value pairs enable us to use each value to represent the count of the number of occurrences for each item (each key). For the result list, since each item occurs twice, our final dictionary would end up as follows: {1:‘2’, 2:‘2’, 3:‘2’, 4:‘2’, 5:‘2’}

Longer Lists

Now we can get on with the one thing we set out to accomplish.

Our solution for counting item occurrences in a Python list presumes some basic familiarity with for loops. We already sneaked for loops into Operation 2 (in our list comprehension, [i*2 for i in my_list]). The loop could also be written as follows:

# multiplying list items
my_list = [1, 2, 3, 4, 5]
# instatiating `result3` as an empty list
result3 = []
# the for loop...
# double each element and append it to a new `result3` list
for i in my_list:
result3.append(i*2)
print(result3)> [2, 4, 6, 8, 10]

The expanded for loop mirrors our earlier use when we updated converted_dict (“for i in result:…”).

In Python, you can expect to encounter lists in which items repeat. In practice, these might involve:

  • college majors, based on a campus survey,
  • homes by price range,
  • or, say, car colors.

We have practiced with lists that have only a few items, but what if we want to count the number of occurrences for each item in a list that looks more like this?:

color_list = ['light blue', 'gold', 'silver', 'blue', 'gold', 'green', 'blue', 'green', 'light blue', 'black', 'gold', 'red', 'light blue', 'light blue', 'silver', 'silver', 'black', 'silver', 'black', 'silver', 'gold', 'green', 'green', 'black', 'silver', 'red', 'black', 'black', 'gold', 'light blue', 'gold', 'black', 'silver', 'black', 'black', 'green', 'light blue', 'green', 'red', 'red', 'green', 'black', 'blue', 'red', 'gold', 'light blue', 'red', 'light blue', 'blue', 'blue', 'blue', 'green', 'red', 'silver', 'blue', 'blue', 'green', 'silver', 'blue', 'green', 'gold', 'blue', 'silver', 'red', 'light blue', 'red', 'gold', 'red', 'gold', 'light blue']

Well, that is precisely what we shall do, next.

Photograph, close-up of iridescent skin of a water python that has recently shed its old skin.
Photo by David Clode on Unsplash

Implementation in Python

Let’s start this example from the beginning.

Step 1: Import the required module.

# importing Python's `random` module
import random
# we will also set a random seed for reproducibility
random.seed(7)

Step 2: Bring in data.

We will instantiate a list of 7 colors. If you already have a list you wish to use, you can skip steps 2–4, in which we create the relatively short, initial list and then randomly append elements from the list to a new, longer list.

# instantiating an alphabetized list of 7 car colors
short_list = ['black', 'blue', 'gold', 'green', 'light blue', 'red', 'silver']

Step 3: Programmatically create an extended, randomized list.

# choosing a random item from `short_list`, seventy times,
# to create a longer `extended_list`, using
# the `random` module’s `.choices()` method

extended_list = random.choices(short_list, k=70)
print (extended_list)> ['gold', 'blue', 'light blue', 'black', 'green', 'gold', 'black', 'green', 'black', 'green', 'black', 'black', 'gold', 'red', 'black', 'blue', 'light blue', 'silver', 'light blue', 'gold', 'silver', 'black', 'silver', 'gold', 'blue', 'black', 'gold', 'red', 'blue', 'light blue', 'light blue', 'gold', 'green', 'black', 'black', 'blue', 'light blue', 'gold', 'gold', 'light blue', 'green', 'gold', 'red', 'light blue', 'blue', 'light blue', 'green', 'silver', 'red', 'gold', 'silver', 'black', 'gold', 'red', 'blue', 'green', 'black', 'light blue', 'red', 'light blue', 'silver', 'gold', 'light blue', 'light blue', 'light blue', 'green', 'red', 'silver', 'green', 'light blue']

Notice that items are no longer in their original order. Items are added to extended_list until we reach seventy items (k=70). Now, it is not so easy to count the number of occurrences of each color on the list.

Step 4: Instantiate a dictionary.

# creating an empty dictionary for our colors
colors_dict = {}

Step 5: Add our colors to a dictionary.

Here, we accomplish several subtasks in a single line of code:

  • We find the length of the list, with Python’s len() method.
  • We use the built-in range() function to get the index for each of the len() items on the list (Recall that Python numbering begins at 0 — when the length of extended_list is 70, the indexes are 0–69).
  • We create a key-value pair in our dictionary, for each indexed list element. Our key is the value of the list element (a string representing a color). The value paired to the key is assigned from the count of how many times that element appears in the list.

See if you can make out the subtasks in the code snippet, below:

# running our loop
for c in range(len(extended_list)):
colors_dict[extended_list[c]] = extended_list.count(
extended_list[c]
)

Step 6: View the dictionary.

Finally, we print our dictionary to see how many of each color is on our list.

# viewing our dictionary
print(colors_dict)
> {'gold': 13, 'blue': 7, 'light blue': 15, 'black': 12, 'green': 9, 'red': 7, 'silver': 7}

Conclusion

Our approach for this article enabled us to introduce or review some of the characteristics of Python lists, including discussing uses for the len() and range() list methods. In addition, we looked at some of the data types that lists can hold, operations that we may perform on them, and how lists are indexed. We then turned our attention to the collection of key-value pairs that make up Python dictionaries. We discussed how dictionaries are structured and explored how unique list elements may be represented as dictionary keys. We also introduced techniques for extending lists and changing the order of list elements. Finally, we completed our todo-list with a walk through a code example, where we used a Python dictionary to present a count of the number of times each color occurs on our extended list.

If you already have a randomized list, to begin with, and leave out all of our related discussion, our approach will enable you to solve this problem in very few lines of code. Of course, there is more than one way to accomplish our task, in Python. What other approaches might you take?

--

--