Code

Collections library for Python

Collections library for Python

Free Python Course ➞ Mini-course for beginners and experienced coders. 4 cool portfolio projects, live communication with the speaker. Click and find out what you can learn in the course.

Learn more

The Python programming language relies heavily on libraries that extend its functionality. Each library can be installed and imported into your project, allowing you to use its capabilities as needed. The most relevant and useful libraries are included in the standard Python installation and are automatically loaded with the interpreter, making them available to developers immediately after installation. This simplifies the programming process and speeds up the development of projects of varying complexity.

The collections library is a powerful tool for working with data in Python. It offers efficient and convenient alternatives to standard data types such as dictionaries, lists, and tuples. With the collections library, you can significantly improve performance and simplify your code by using data structures such as deque, defaultdict, and Counter. These tools enable developers to manage data more efficiently by optimizing insertion, deletion, and search operations. The collections library is indispensable for those seeking to improve the quality and speed of data processing in their Python projects.

The collections library is an indispensable tool for developers and data scientists working with data. It significantly simplifies the tasks of calculating values, organizing collections, and quickly converting data from CSV files and SQL databases. Many professionals note that after becoming familiar with this library, they ask themselves, "How did I ever get by without it?" Using collections allows you to optimize your workflows and improve the efficiency of data processing.

Image: Mosfilm Studio

defaultdict: an error-free dictionary

A standard dict dictionary in Python contains key:value pairs. A dictionary value can be queried by key. If the requested key isn't in the dictionary, Python will throw an error:

The behavior of Python's standard dictionary can be logical, but sometimes it's not entirely convenient to work with. In this context, it's worth considering the defaultdict dictionary from the collections module. This enhanced dictionary behaves differently: if a key that isn't in the dictionary is requested, it doesn't raise an error but returns a default value, avoiding unnecessary exceptions and simplifying data processing. This makes defaultdict an excellent tool for situations where you need to work with dynamically changing data sets, where some keys may be missing.

  • create this key;
  • create a value for it, which will have a default type (hence the name), such as an integer or a string;
  • and — ta-da! — return this value to you.

Python has standard dictionary methods, such as .setdefault() and .get(), which provide similar functionality. However, using them can be more complicated because you need to specify a default value each time. In contrast, in this case, we can define the type for all values ​​at once, which simplifies working with the dictionary. After that, it is enough to simply access the values ​​by key, which makes the code more readable and easier to use.

How does this process work?

The first line imports the defaultdict module from the collections library. This module provides a convenient way to create dictionaries with default values. Using defaultdict simplifies working with collections of data by avoiding errors related to missing keys. This is especially useful when handling data where it is not known in advance which keys might be used.

In the second example, we converted our previous dictionary my_dict into a new dictionary my_new_dict. The "int" data type specified in parentheses means that the values ​​created by default will be integers, specifically zero.

Now we print our my_new_dict to get acquainted with its contents. The Python interpreter displays the following information:

  • dictionary type: defaultdict;
  • default value type: int;
  • as well as the current contents of our dictionary. The key "d" is not there, just like in the original dictionary my_dict.

In the next line, we request an element by the specified key. And, lo and behold, my_new_dict returns a value of 0.

Notice the updated contents of our dictionary, where a new pair ‘d’: 0 has appeared. This change may indicate the introduction of new data or the modification of existing values, which is important for analyzing and working with data in our project. You should consider how this new entry may affect further operations with the dictionary, including searching, updating, and deleting elements. Optimizing the data structure will help improve the performance and efficiency of information processing.

When creating a defaultdict dictionary, you can use various standard data types instead of int, such as float, list, str, and other dict dictionaries. This allows you to automatically create new elements the first time a key corresponding to the specified type is accessed. For example, when using float, new elements will be initialized to zero values, for list - to empty lists, for str - to empty strings, and for dict - to empty dictionaries. This approach simplifies working with data and increases flexibility in processing it.

The defaultdict dictionary in Python provides all the methods available in standard dictionaries, such as .items(), .values(), .keys(), and others. This makes it a convenient tool for working with data collections, allowing you to effectively manage missing values ​​and avoid errors when accessing non-existent keys. Using defaultdict is especially useful when you need to initialize default values ​​for new keys, significantly simplifying your code and improving its readability.

namedtuple: tuple, just named

A tuple in Python, often called an "immutable list," is a data structure that holds a fixed number of elements. Each element of the tuple can be accessed by its index. Tuples are useful for storing data that shouldn't change during program execution, making them ideal for working with constants or groups of related values.

For simple tasks, tuples are perfectly reasonable; however, as the number of elements in a tuple increases, index access can significantly complicate the code. This negatively impacts its readability and maintainability. Therefore, when working with large data sets, it is worth considering alternative data structures that provide more convenient access and management of elements.

The Python collections library offers a useful tool: the namedtuple function. It extends standard tuples by allowing each tuple element to have its own name. This creates named fields that can conveniently store values, reminiscent of the row structure in a table. Using namedtuple makes code more readable and manageable, which is especially important when working with large amounts of data.

As a result, we obtain a dictionary that is immutable but also more readable. This approach simplifies interaction with data and improves code usability, which is especially important in software development.

We imported the namedtuple function from the collections module. Then, we created a namedtuple constructor called fruit, specifying the necessary parameters. This allows us to conveniently work with data, providing access to elements by name, which makes the code more readable and easier to use. Using namedtuples is an effective way to structure data in Python.

  • the general name of the tuples we will be creating (‘fruit’);
  • a string containing the names of the namedtuple fields. The names are separated in the string by a space.

After this, two namedtuples were created: apple and orange. These tuples allow you to conveniently store and process data by associating values ​​with clear names. Using namedtuples improves the readability of code and makes it more understandable for developers, which is especially important when working with large amounts of information.

Now you can access the elements of these namedtuples not by indexes, but by the names of the fields: number, sort, and color. These fields are attributes of namedtuples and can be accessed using a dot, without the need for parentheses. This approach simplifies working with data and makes code more readable and understandable.

Named tuples retain all the characteristics of simple tuples, including high performance and immutability. They also support the methods inherent in regular tuples. The official documentation provides high-quality examples of their use. Named tuples are convenient for creating structured data and simplify access to elements by name, which makes code more readable and understandable.

Counter: Count Everything

Counter is a special dictionary class in Python designed for counting objects. As the name suggests, its main function is to keep track of the number of distinct elements. In this class, the keys are the elements to be counted, and the values ​​are the number of these elements. Let's look at some examples of using Counter to gain a deeper understanding of its capabilities.

The most common letter in the text is "b", which appears 8 times. In a list of numbers, the most common letters are 5 and 7, which appear 3 times each. Counting words in a sentence can be simplified by focusing on the number of items in the list.

Counter provides all the standard dictionary methods, plus the additional elements() and most_common(n) functions. The elements() method returns a list of items whose count is greater than zero, while most_common(n) returns n pairs of the most common items as tuples, where each tuple consists of an item and its count. These functions make working with item counts more convenient and efficient.

Typical examples of using the Counter() function allow you to effectively count the number of items in data collections. Counter() is a convenient tool for analyzing item frequencies, which is especially useful in data processing and text analysis tasks.

With Counter(), you can easily count the number of occurrences of words in text, the frequency of characters in a string, or even the number of unique values ​​in a list. This function supports working with various data types, including strings, lists, and tuples, making it a versatile solution for statistical analysis.

By using Counter(), you can quickly get a summary of the data distribution, which will help in further analysis and decision-making. For example, in text analytics, you can use Counter() to identify the most frequently used words, which can be useful in SEO content optimization.

Thus, using the Counter() function significantly simplifies the process of counting and analyzing data, allowing you to focus on interpreting the results and making informed decisions based on the information obtained.

What's Next

The tools from the collections library provide efficient work with large volumes of similar data. They allow you to quickly process, transform, and transfer information. The use of collections is especially relevant in cases where high speed of operations, reliable data accounting, and code readability are important. Use this library to optimize your projects and improve their performance.

In the "Becoming a Python Developer" course, you'll learn key Python libraries that will become indispensable tools in your studies and career. These libraries will help you develop your programming skills and improve your competitiveness in the job market. Join us and discover the world of possibilities that Python offers.