Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

All about Collections Module in Python
Latest   Machine Learning

All about Collections Module in Python

Last Updated on November 5, 2023 by Editorial Team

Author(s): Muttineni Sai Rohith

Originally published on Towards AI.

As we all know, python has its own regular data type heroes β€” List, Tuple, Dictionary, and infamous Sets. But along with this, the Justice League of Python has its Superman β€” named Collections. In this article, we are going to dive deep into the world of collections and understand the use cases and advantages of it.

Collections

The collections module in Python implements a set of specialized container datatypes, providing alternatives to Python’s built-in datatypes like list, tuple, set, and Dictionary.

Let’s understand the importance of each functionality in the collections module.

namedTuple

is a module in collections that provides us with an easy way to create a class for storing structured data without defining a full-fledged class.
Instead of accessing Tuples with indexes, they are converted into a class and we can access them based on each identifier's names instead of indexes.

namedTuple

In the example above, in line 2, we are creating a namedTuple by name β€” Employee which holds the name, id, and age of the Employee. This will create a class variable that can be used to store Employee records.

namedTuple β€” 2

namedTuples can also be accessed using indexes, as shown in line 3 they can be easily converted to a dictionary and also support Tuple unpacking.

Counter

Counter, as the name suggests, is used for counting the occurrences of elements in the collections β€” list, tuples, and string. It returns a dictionary with a key as an element and a value as the count of occurrences of a particular element.

Counter

As shown in the example, Counter returned occurrences of each particular in the list in dictionary format. most_common(k) method of Counter returns k most repeated occurrence. We can also perform addition, subtraction, intersection, and union of Counters.

deque

usually referred to as a deck, is a short form of double-end-queue. It serves as an optimized list to perform insertion and deletion of elements from both ends easily with O(1) complexity.

deque

As shown above, elements can be added on both ends easily by using the append and append-left methods.

deque β€” 2

similar to list, deque provides reverse, rotate operations and pop, pop left methods to remove elements from both ends.

ChainMap

is used to combine multiple dictionaries into a single view without copying the data. As the name suggests, the mappings(dictionaries) are organized into a chain, and we can access and search them in the same order. If a key exists in both the dictionaries, then the latest dictionary in the chain is always given preference.

ChainMap

defaultdict

is used when we want to create a dictionary with default values for missing keys. The difference between a dictionary and a defaultdict is when we use defaultdict and try to access a missing key instead of raising key error, it will return the specified default value.

with dictionary
defaultdict

Ordereddict

is a dictionary that remembers the order in which the elements are entered. when we try to access the OrderedDict it will return elements in the order in which they are inserted.

OrderedDict

UserDict

UserDict is useful when we want to create a dictionary with custom behavior. Suppose we want to create a dictionary where values entered are uppercased automatically, then UserDict comes to your rescue.

UserDict

Same applies for UserList and UserString.

Sharing colab notebook for code.

Happy Learning…..

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.

Published via Towards AI

Feedback ↓