Python Forum
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())))

  1. get all values of the dict with my_dict.values(). We don't need the keys.
  2. chain the values, that you've one long sequence. itertools.chain can do it.
    chain.from_iterable is a different constructor for convenience.
  3. dict.values(), chain.from_iterable() are lazy iterators and must be consumed. In this example I use a list to consume the results.
  4. len will count the elements in the list, which has been created before.
  5. You've got your count.


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:
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)


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)