Posts: 13
Threads: 7
Joined: Oct 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
Posts: 1,838
Threads: 2
Joined: Apr 2017
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.
Posts: 13
Threads: 7
Joined: Oct 2020
Oct-26-2020, 08:59 AM
(This post was last modified: Oct-26-2020, 08:59 AM by Inkanus.)
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?
Posts: 1,583
Threads: 3
Joined: Mar 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.
Posts: 8,155
Threads: 160
Joined: Sep 2016
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?
Posts: 24
Threads: 2
Joined: Sep 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)
Posts: 2,122
Threads: 10
Joined: May 2017
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())))
- get all values of the dict with
my_dict.values() . We don't need the keys.
- chain the values, that you've one long sequence.
itertools.chain can do it.
chain.from_iterable is a different constructor for convenience.
dict.values() , chain.from_iterable() are lazy iterators and must be consumed. In this example I use a list to consume the results.
- len will count the elements in the list, which has been created before.
- 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.
Posts: 13
Threads: 7
Joined: Oct 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)
|