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
#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


Messages In This Thread
RE: Counting the values ​​in the dictionary - by Inkanus - Oct-26-2020, 01:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using Lists as Dictionary Values bfallert 4 106 19 minutes ago
Last Post: bfallert
  need to compare 2 values in a nested dictionary jss 2 855 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Printing specific values out from a dictionary mcoliver88 6 1,386 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,736 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  Getting values from a dictionary brunolelli 5 3,573 Mar-31-2021, 11:57 PM
Last Post: snippsat
  Python dictionary with values as list to CSV Sritej26 4 3,008 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 3,977 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
  Adding keys and values to a dictionary giladal 3 2,478 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  In this dictionary all the values end up the same. How? Pedroski55 2 1,927 Oct-29-2020, 12:32 AM
Last Post: Pedroski55
  How to make a list of values from a dictionary list? faryad13 2 2,067 Sep-03-2020, 03:45 PM
Last Post: faryad13

Forum Jump:

User Panel Messages

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