Why are dictionaries more generally useful for counting the occurrences of values in some collection of values than lists?

Counting the frequencies in a list using dictionary in Python

Given an unsorted list of some elements(may or may not be integers), Find the frequency of each distinct element in the list using a dictionary.
Example:

Input : [1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2] Output : 1 : 5 2 : 4 3 : 3 4 : 3 5 : 2 Explanation : Here 1 occurs 5 times, 2 occurs 4 times and so on...

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The problem can be solved in many ways. A simple approach would be to iterate over the list and use each distinct element of the list as a key of the dictionary and store the corresponding count of that key as values. Below is the Python code for this approach:

Python




# Python program to count the frequency of
# elements in a list using a dictionary
def CountFrequency(my_list):
# Creating an empty dictionary
freq = {}
for item in my_list:
if (item in freq):
freq[item] += 1
else:
freq[item] = 1
for key, value in freq.items():
print ("% d : % d"%(key, value))
# Driver function
if __name__ == "__main__":
my_list =[1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2]
CountFrequency(my_list)
Output: 1 : 5 2 : 4 3 : 3 4 : 3 5 : 2

Time Complexity:O(N), where N is the length of the list.



Alternative way: An alternative approach can be to use the list.count() method.

Python




# Python program to count the frequency of
# elements in a list using a dictionary
def CountFrequency(my_list):
# Creating an empty dictionary
freq = {}
for items in my_list:
freq[items] = my_list.count(items)
for key, value in freq.items():
print ("% d : % d"%(key, value))
# Driver function
if __name__ == "__main__":
my_list =[1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2]
CountFrequency(my_list)
Output: 1 : 5 2 : 4 3 : 3 4 : 3 5 : 2

Time Complexity:O(N2), where N is the length of the list. The time complexity list.count() is O(N) alone, and when used inside loop it will become O(N2).

Alternative way:An alternative approach can be to use the dict.get() method. This makes the program much more shorter and makes understand how get method is useful instead of if…else.

Python




# Python program to count the frequency of
# elements in a list using a dictionary
def CountFrequency(my_list):
# Creating an empty dictionary
count = {}
for i in [1, 1, 1, 5, 5, 3, 1, 3, 3, 1 ,4, 4, 4, 2, 2, 2, 2]:
count[i] = count.get(i, 0) + 1
return count
# Driver function
if __name__ == "__main__":
my_list =[1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2]
print(CountFrequency(my_list))
Output: {1: 5, 5: 2, 3: 3, 4: 3, 2: 4}

Related Article :
Count frequencies of all elements in array in Python using collections module

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
frequency-counting
Python dictionary-programs
python-dict
python-list
Practice Tags :
python-dict
python-list
Read Full Article

Dictionaries

A dictionary is like a list, but more general. In a list, the index positions have to be integers; in a dictionary, the indices can be (almost) any type.

You can think of a dictionary as a mapping between a set of indices (which are called keys) and a set of values. Each key maps to a value. The association of a key and a value is called a key-value pair or sometimes an item.

As an example, we’ll build a dictionary that maps from English to Spanish words, so the keys and the values are all strings.

The function dict creates a new dictionary with no items. Because dict is the name of a built-in function, you should avoid using it as a variable name.

>>> eng2sp = dict() >>> print(eng2sp) {}

The curly brackets, {}, represent an empty dictionary. To add items to the dictionary, you can use square brackets:

>>> eng2sp['one'] = 'uno'

This line creates an item that maps from the key 'one' to the value “uno”. If we print the dictionary again, we see a key-value pair with a colon between the key and value:

>>> print(eng2sp) {'one': 'uno'}

This output format is also an input format. For example, you can create a new dictionary with three items. But if you print eng2sp, you might be surprised:

>>> eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'} >>> print(eng2sp) {'one': 'uno', 'three': 'tres', 'two': 'dos'}

The order of the key-value pairs is not the same. In fact, if you type the same example on your computer, you might get a different result. In general, the order of items in a dictionary is unpredictable.

But that’s not a problem because the elements of a dictionary are never indexed with integer indices. Instead, you use the keys to look up the corresponding values:

>>> print(eng2sp['two']) 'dos'

The key 'two' always maps to the value “dos” so the order of the items doesn’t matter.

If the key isn’t in the dictionary, you get an exception:

>>> print(eng2sp['four']) KeyError: 'four'

The len function works on dictionaries; it returns the number of key-value pairs:

>>> len(eng2sp) 3

The in operator works on dictionaries; it tells you whether something appears as a key in the dictionary (appearing as a value is not good enough).

>>> 'one' in eng2sp True >>> 'uno' in eng2sp False

To see whether something appears as a value in a dictionary, you can use the method values, which returns the values as a type that can be converted to a list, and then use the in operator:

>>> vals = list(eng2sp.values()) >>> 'uno' in vals True

The in operator uses different algorithms for lists and dictionaries. For lists, it uses a linear search algorithm. As the list gets longer, the search time gets longer in direct proportion to the length of the list. For dictionaries, Python uses an algorithm called a hash table that has a remarkable property: the in operator takes about the same amount of time no matter how many items there are in a dictionary. I won’t explain why hash functions are so magical, but you can read more about it at wikipedia.org/wiki/Hash_table.

Exercise 1: Download a copy of the file www.py4e.com/code3/words.txt

Write a program that reads the words in words.txt and stores them as keys in a dictionary. It doesn’t matter what the values are. Then you can use the in operator as a fast way to check whether a string is in the dictionary.

That One Thing

Create a Dictionary From a List

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

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…

Lists, tuples, sets, dictionaries... When do you use which built-in data structure?

Python is an object-oriented programming (OOP) language. Classes and objects are used to structure and modularize code to be reusable and easy to modify. OOP requires the use of data structures to organize and store data in a way that can be efficiently accessed.

Python has primitive (or basic) data structures such as floats, integers, strings, and Booleans. Python also has non-primitive data structures such as lists, tuples, dictionaries, and sets. Non-primitive data structures store a collection of values in various formats rather than a single value. Some can hold data structures within the data structure, creating depth and complexity in data storage capabilities.

In this article, we will look into each built-in data structure to decide when it is appropriate to use one over another.

Mutability means that the data in the data structure can be modified (added, deleted, or changed) after creation. Mutability is an important factor to consider when choosing your data structure. If you know that you won’t need to change the internal state, consider using an immutable object to ensure that it is thread-safe and that nothing can overwrite your data.

To represent a sequence of items indexed by their integer position, one data structure you can use is a list. Lists contain zero or more elements and can contain elements of different types (even objects!). This makes lists powerful because they allow you to create deep and complex data structures.

Lists are mutable, meaning that you can add, delete, or change elements in a flexible manner. Another sequential data structure is a tuple; the difference between these two is that tuples are immutable.

Because lists have a sequential element: if you only want to keep track of unique values and don’t care about the order, use a Python set.

Create lists using [] or list(). Typecast using list().

Access list items:
my_list[0] gets a list item by offset. Like strings, negative indexes can be used to count backward from the end.
my_list[0] = ‘new item' changes a list item by offset.
my_list[0:2] slices to extract items by offset. This example returns the first 2 elements of my_list.

Add list items:
append() adds an item to the end of a list.
extend() or += merges one list into another.
insert() adds an item before any offset.

Remove list items:
remove() removes an item value from a list.
pop() removes the last (or specified) element while also returning the value.
del removes an item by its position in the list. del is a Python statement, not a list method.

join() returns a string of combined list items. The argument for join() is a string or any iterable sequence of strings.
len() returns the number of items in the list. count() returns the number of occurrences of a specified value.

Tuples are also a sequenced data structure, just like lists. However, tuples are immutable; you cannot add, delete, or change items after a tuple is created. Tuples differ from lists by having many fewer functions because they can’t be modified after being defined. Tuples contain zero or more elements and can contain elements of different, immutable types.

Advantages to tuples over lists:

Create tuples using () or a comma-separated list of elements with no surrounding brackets or braces. Typecast using tuple().

count() returns the number of times an element is found in the tuple
index() returns the index position of an element

Instead of using an offset, dictionaries use keys to associate with each value. This means that order is not tracked and should not matter if you plan to use a dictionary. Dictionary keys are immutable and unique, however, dictionaries are mutable; the key-value elements can be added, deleted, or changed. In short, dictionaries are very similar to hashmaps.

Create dictionaries using {}. Typecast using dict().

my_dict[‘key’] gets an item by its key
my_dict['key'] = ‘value' uses a key to add (or change if it already exists) a value.
update() merges the keys and values of one dictionary into another.
del deletes an item by the provided key. del is a Python statement, not a dictionary method.
keys() returns all the dictionary keys. values() returns all the values in the dictionary. items() returns all the dictionary key-value pairs.

A set is like a dictionary with only the keys, not the values. This means that sets are unique and not sequential (stored unordered). Sets are also mutable. Sets contain zero or more elements and can contain elements of different, immutable types.

Essentially, sets are used when you want to know if something exists and nothing else about it. If it matters to track value order or store multiple of the same value, consider using a space-friendly tuple instead.

Create sets using set(). Typecast using set().

add() adds an item to the set if it doesn’t already exist
clear() removes all items from the set
intersect() returns an intersection of two sets
union() returns a union of two sets

Background

Lists are an increasingly popular topic for those who are starting to learn Python as well as for those who are already experienced with the language. If we believe the search results in Google Trends, the search interest in this topic has been rising each and every year.

If you are a regular visitor to forums to answer or ask questions about Python programming, such as Stack Overflow, Quora or Reddit, you might know the reason behind it.

A lot of Python questions find their ways to these forums and continue to persist there, where users mark some as ‘duplicate’ or ‘sticky’, upvote them or discuss the right solution with others.

With this blog post, DataCamp wants to help you to tackle one topic, namely, the most frequently asked questions about lists in Python, and this in an interactive way!

Python's Counter: The Pythonic Way to Count Objects

by Leodanis Pozo Ramos Jul 05, 2021 basics python
Mark as Completed
Tweet Share Email

Table of Contents

Remove ads

Counting several repeated objects at once is a common problem in programming. Python offers a bunch of tools and techniques you can use to approach this problem. However, Python’s Counter from collections provides a clean, efficient, and Pythonic solution.

This dictionary subclass provides efficient counting capabilities out of the box. Understanding Counter and how to use it efficiently is a convenient skill to have as a Python developer.

In this tutorial, you’ll learn how to:

  • Count several repeated objects at once
  • Create counters with Python’s Counter
  • Retrieve the most common objects in a counter
  • Update object counts
  • Use Counter to facilitate further computations

You’ll also learn about the basics of using Counter as a multiset, which is an additional feature of this class in Python.

Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.

Video liên quan

Postingan terbaru

LIHAT SEMUA