![]() |
Counting the values in the dictionary - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Counting the values in the dictionary (/thread-30562.html) |
Counting the values in the dictionary - Inkanus - Oct-26-2020 Hi. I have some problem with dictionary. My dictionary has two keys, and each key has three values assigned to it. I would like to count the amount of values. I don't know what to apply to get six dict = { "key1": ["a","b", "c"], "key2": ["x","y", "z"] } Do you have some ideas? Greetings RE: Counting the values in the dictionary - ndc85430 - Oct-26-2020 Do you know how to iterate over a dictionary? Do you know how to get just the values in a dictionary? If not, those are things you can look up and should at least help you make some headway. RE: Counting the values in the dictionary - Inkanus - Oct-26-2020 I did that simple iteration for i in dict: print("I will go to", i, "and i will buy:", dict[i] And it's good. The keys and their values are printed is this how to count all the values? RE: Counting the values in the dictionary - bowlofred - Oct-26-2020 Quote:My dictionary has two keys, and each key has three values assigned to it Not quite. Each key has one value. It's just that in your case, you have containers for the values, and those containers have have 3 elements. If you're sure they're always lists, you can get the size with len(). Loop over the values and count the size of each value with len(). You'll get the total number of elements. RE: Counting the values in the dictionary - buran - Oct-26-2020 what do you mean by count :count total number of elements in all lists? count number of elements in each list count number of occurrences of each element in all lists? RE: Counting the values in the dictionary - Askic - Oct-26-2020 I'd do something like this: dict1 = { "key1": ["a", "b", "c"], "key2": ["x", "y", "z"] } count = 0 for j in dict1.values(): count += len(j) print(count)Or the way you started: count = 0 for i in dict1: print("I will go to", i, "and i will buy:", dict1[i]) count += len(dict1[i]) print(count) RE: Counting the values in the dictionary - DeaD_EyE - Oct-26-2020 If you want to sum the count of all existing value: from itertools import chain # chain chains iterables # don't use dict as name, because # dict is a type my_dict = { "key1": ["a","b", "c"], "key2": ["x","y", "z"], } count = len(list(chain.from_iterable(my_dict.values())))
The contra of this method is, that a new list is created, then counted and then the list is garbage collected. If the data is massive, you can use a regular for-loop to count. This will reduce the memory footprint. my_dict = { "key1": ["a","b", "c"], "key2": ["x","y", "z"], } count = 0 for values in my_dict.values(): count += len(values)If you have a sequence and you want to count equal elements in this sequence, you can use collections.Counter .It's very handy to get the most_common. RE: Counting the values in the dictionary - Inkanus - Oct-26-2020 (Oct-26-2020, 09:38 AM)Askic Wrote: I'd do something like this: Thanks, that was exactly what I was looking for. I am sorry if I did not express myself clearly, I meant to specifically count these six elements (a, b, c, x, y, z) |