Python Forum
Counting the values ​​in the dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting the values ​​in the dictionary
#1
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
Reply
#2
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.
Reply
#3
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?
Reply
#4
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.
Inkanus likes this post
Reply
#5
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?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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)
Inkanus likes this post
Reply
#7
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.
Inkanus likes this post
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
(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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting values from a dictionary brunolelli 5 416 Mar-31-2021, 11:57 PM
Last Post: snippsat
  Python dictionary with values as list to CSV Sritej26 4 334 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 576 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
  Adding keys and values to a dictionary giladal 3 352 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  In this dictionary all the values end up the same. How? Pedroski55 2 351 Oct-29-2020, 12:32 AM
Last Post: Pedroski55
  How to make a list of values from a dictionary list? faryad13 2 504 Sep-03-2020, 03:45 PM
Last Post: faryad13
  get method not counting number of strings in dictionary LearningTocode 2 523 Jun-13-2020, 11:17 PM
Last Post: LearningTocode
  access dictionary with keys from another and write values to list redminote4dd 6 913 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  Transforming nested key-tuples into their dictionary values ClassicalSoul 4 837 Apr-11-2020, 04:36 PM
Last Post: bowlofred
  Can't unpack values of dictionary with ** Snake 3 1,012 Mar-11-2020, 11:17 AM
Last Post: Snake

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020