Get Maximum of Each Key Dictionary List - Python Last Updated : 05 Feb, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given a list of dictionaries and our task is to find the maximum value for each key across all dictionaries. If a key appears in multiple dictionaries we take the highest value among them. For example: a = [{'a': 3, 'b': 8}, {'a': 10, 'b': 2, 'c': 5}, {'c': 12, 'a': 7}] then the output will be {'a': 10, 'b': 8, 'c': 12}Using defaultdictWe can use defaultdict(int) to store the maximum values for each key while iterating through the list of dictionaries. Python from collections import defaultdict a = [{'a': 3, 'b': 8}, {'a': 10, 'b': 2, 'c': 5}, {'c': 12, 'a': 7}] res = defaultdict(int) for d in a: for k, v in d.items(): res[k] = max(res[k], v) print(dict(res)) Output{'a': 10, 'b': 8, 'c': 12} Explanation:We use defaultdict(int) which initializes missing keys with 0 (since int() returns 0).We iterate through each dictionary and compare each key’s value with the existing maximum then the max() function ensures that only the highest value is stored for each key.Using CounterWe can use collections.Counter to store and update the maximum values dynamically. Python from collections import Counter a = [{'a': 3, 'b': 8}, {'a': 10, 'b': 2, 'c': 5}, {'c': 12, 'a': 7}] res = Counter() for d in a: for k, v in d.items(): res[k] = max(res[k], v) print(dict(res)) Output{'a': 10, 'b': 8, 'c': 12} Explanation:Counter behaves like a dictionary but starts with default values of 0, similar to defaultdict(int).We iterate through each dictionary and update the maximum value for each key.max_values[k] = max(max_values[k], v) ensures that the highest value is always stored.Using Dictionary ComprehensionWe can use dictionary comprehension combined with max() to find the maximum value for each key. Python a = [{'a': 3, 'b': 8}, {'a': 10, 'b': 2, 'c': 5}, {'c': 12, 'a': 7}] res = {k: max(d.get(k, float('-inf')) for d in a) for k in {key for d in a for key in d}} print(res) Output{'a': 10, 'c': 12, 'b': 8} Using pandas.DataFrameIf the dictionary list is large then we can use Pandas to efficiently compute the maximum for each key. Python import pandas as pd a = [{'a': 3, 'b': 8}, {'a': 10, 'b': 2, 'c': 5}, {'c': 12, 'a': 7}] df = pd.DataFrame(arr) res = df.max().to_dict() print(res) {'a': 10.0, 'b': 8.0, 'c': 12.0}Explanation:We create a Pandas DataFrame from the list of dictionaries and df.max() finds the maximum values for each column (key)..to_dict() converts the result back into a dictionary. Comment More infoAdvertise with us Next Article Get List of Values From Dictionary - Python M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Python dictionary-programs Practice Tags : python Similar Reads Get List of Values From Dictionary - Python We are given a dictionary and our task is to extract all the values from it and store them in a list. For example, if the dictionary is d = {'a': 1, 'b': 2, 'c': 3}, then the output would be [1, 2, 3].Using dict.values()We can use dict.values() along with the list() function to get the list. Here, t 2 min read Dictionary keys as a list in Python In Python, we will encounter some situations where we need to extract the keys from a dictionary as a list. In this article, we will explore various easy and efficient methods to achieve this.Using list() The simplest and most efficient way to convert dictionary keys to lists is by using a built-in 2 min read Get first K items in dictionary = Python We are given a dictionary and a number K, our task is to extract the first K key-value pairs. This can be useful when working with large dictionaries where only a subset of elements is needed. For example, if we have: d = {'a': 1, 'b': 2, 'c': 3, 'd': 4} and K = 2 then the expected output would be: 2 min read Python - Extracting Kth Key in Dictionary Many times, while working with Python, we can have a situation in which we require to get the Kth key of dictionary. There can be many specific uses of it, either for checking the indexing and many more of these kind. This is useful for Python version 3.8 +, where key ordering are similar as inserti 4 min read Get all Unique Keys from a List of Dictionaries - Python Our task is to get all unique keys from a list of dictionaries and we are given a list where each element is a dictionary, we need to extract and return a list of keys that appear across all dictionaries. The result should contain each key only once regardless of how many times it appears. For examp 3 min read Get Index of Values in Python Dictionary Dictionary values are lists and we might need to determine the position (or index) of each element within those lists. Since dictionaries themselves are unordered (prior to Python 3.7) or ordered based on insertion order (in Python 3.7+), the concept of "index" applies to the valuesâspecifically whe 3 min read Like