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
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
  need to compare 2 values in a nested dictionary jss 2 797 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Printing specific values out from a dictionary mcoliver88 6 1,317 Apr-12-2023, 08:10 PM
Last Post: deanhystad
Question How to print each possible permutation in a dictionary that has arrays as values? noahverner1995 2 1,695 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  Getting values from a dictionary brunolelli 5 3,518 Mar-31-2021, 11:57 PM
Last Post: snippsat
  Python dictionary with values as list to CSV Sritej26 4 2,957 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 3,905 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
  Adding keys and values to a dictionary giladal 3 2,429 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  In this dictionary all the values end up the same. How? Pedroski55 2 1,896 Oct-29-2020, 12:32 AM
Last Post: Pedroski55
  How to make a list of values from a dictionary list? faryad13 2 2,015 Sep-03-2020, 03:45 PM
Last Post: faryad13
  get method not counting number of strings in dictionary LearningTocode 2 2,044 Jun-13-2020, 11:17 PM
Last Post: LearningTocode

Forum Jump:

User Panel Messages

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