Python - Counter.items(), Counter.keys() and Counter.values() Last Updated : 10 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Counter class is a special type of object data-set provided with the collections module in Python3. Collections module provides the user with specialized container datatypes, thus, providing an alternative to Python’s general-purpose built-ins like dictionaries, lists and tuples. Counter is a sub-class that is used to count hashable objects. It implicitly creates a hash table of an iterable when invoked. Counter.items()The Counter.items() method helps to see the elements of the list along with their respective frequencies in a tuple. Syntax : Counter.items()Parameters : NoneReturns : object of class dict.values() Example : Python3 # importing the module from collections import Counter # making a list list = [1, 1, 2, 3, 4, 5, 6, 7, 9, 2, 3, 4, 8] # instantiating a Counter object ob = Counter(list) # Counter.items() items = ob.items() print("The datatype is " + str(type(items))) # displaying the dict_items print(items) # iterating over the dict_items for i in items: print(i) Output : The datatype is dict_items([(1, 2), (2, 2), (3, 2), (4, 2), (5, 1), (6, 1), (7, 1), (9, 1), (8, 1)]) (1, 2) (2, 2) (3, 2) (4, 2) (5, 1) (6, 1) (7, 1) (9, 1) (8, 1) Counter.keys()The Counter.keys() method helps to see the unique elements in the list. Syntax : Counter.keys()Parameters : NoneReturns : object of class dict_items Example : Python3 # importing the module from collections import Counter # making a list list = [1, 1, 2, 3, 4, 5, 6, 7, 9, 2, 3, 4, 8] # instantiating a Counter object ob = Counter(list) # Counter.keys() keys = ob.keys() print("The datatype is " + str(type(keys))) # displaying the dict_items print(keys) # iterating over the dict_items for i in keys: print(i) Output : The datatype is dict_keys([1, 2, 3, 4, 5, 6, 7, 9, 8]) 1 2 3 4 5 6 7 9 8 Counter.values()The Counter.values() method helps to see the frequencies of each unique element. Syntax : Counter.values()Parameters : NoneReturns : object of class dict_items Example : Python3 # importing the module from collections import Counter # making a list list = [1, 1, 2, 3, 4, 5, 6, 7, 9, 2, 3, 4, 8] # instantiating a Counter object ob = Counter(list) # Counter.values() values = ob.values() print("The datatype is " + str(type(values))) # displaying the dict_items print(values) # iterating over the dict_items for i in values: print(i) Output : The datatype is dict_values([2, 2, 2, 2, 1, 1, 1, 1, 1]) 2 2 2 2 1 1 1 1 1 Comment More infoAdvertise with us Next Article Python - Counter.items(), Counter.keys() and Counter.values() S sangy987 Follow Improve Article Tags : Python Python collections-module Practice Tags : python Similar Reads Python | Count number of items in a dictionary value that is a list In Python, dictionary is a collection which is unordered, changeable and indexed. Dictionaries are written with curly brackets, and they have keys and values. It is used to hash a particular key. A dictionary has multiple key:value pairs. There can be multiple pairs where value corresponding to a ke 5 min read How to Add Same Key Value in Dictionary Python Dictionaries are powerful data structures that allow us to store key-value pairs. However, one common question that arises is how to handle the addition of values when the keys are the same. In this article, we will see different methods to add values for the same dictionary key using Python.Adding 2 min read Python - Frequencies of Values in a Dictionary Sometimes, while working with python dictionaries, we can have a problem in which we need to extract the frequency of values in the dictionary. This is quite a common problem and has applications in many domains including web development and day-day programming. Let's discuss certain ways in which t 4 min read Pandas Index.value_counts()-Python Python is popular for data analysis thanks to its powerful libraries and Pandas is one of the best. It makes working with data simple and efficient. The Index.value_counts() function in Pandas returns the count of each unique value in an Index, sorted in descending order so the most frequent item co 3 min read How to Add Duplicate Keys in Dictionary - Python In Python, dictionaries are used to store key-value pairs. However, dictionaries do not support duplicate keys. In this article, we will explore several techniques to store multiple values for a single dictionary key.Understanding Dictionary Key ConstraintsIn Python, dictionary keys must be unique. 3 min read Python dictionary values() values() method in Python is used to obtain a view object that contains all the values in a dictionary. This view object is dynamic, meaning it updates automatically if the dictionary is modified. If we use the type() method on the return value, we get "dict_values object". It must be cast to obtain 2 min read Python | Count occurrences of an element in a Tuple In this program, we need to accept a tuple and then find the number of times an item is present in the tuple. This can be done in various ways, but in this article, we will see how this can be done using a simple approach and how inbuilt functions can be used to solve this problem. Examples: Tuple: 3 min read Python collections Counter Counters are a subclass of the dict class in Python collections module. They are used to count the occurrences of elements in an iterable or to count the frequency of items in a mapping. Counters provide a clean and efficient way to tally up elements and perform various operations related to countin 4 min read Python - Access Dictionary items A dictionary in Python is a useful way to store data in pairs, where each key is connected to a value. To access an item in the dictionary, refer to its key name inside square brackets.Example:Pythona = {"Geeks": 3, "for": 2, "geeks": 1} #Access the value assosiated with "geeks" x = a["geeks"] print 3 min read Like