-4

I have a dictionary in a below format:

folders = {'Master folder': ['Training'], 'Child folder': ['Training videos', 'Training documents', 'Training workouts', 'Training practicals']}

I want to count the values as below is expected,

count_of_masterfolder = 1
count_of_childfolder = 4

So far I did this,

    co_master = folders.items['Master folder']
    co_child = folders.items['Child folder']
    print(co_master, co_child)

Am getting this error:

    co = folders.items['Master folder']
TypeError: 'builtin_function_or_method' object is not subscriptable

What should I do?

milanbalazs
  • 4,811
  • 4
  • 23
  • 45
ilexcel
  • 895
  • 8
  • 12
  • That's not how you index a dictionary - just use `folders[key]`. `items` is a function that returns a view object / list of all key-value pairs in the dictionary. – meowgoesthedog Aug 05 '19 at 15:38
  • this doesnt returns me my value, as I said in expected section, I need to store each count values in separate variables and print – ilexcel Aug 05 '19 at 15:41

4 Answers4

1

Do you mean you want count_of_masterfolder = 1 etc. as the output of your program, or do you want the count be assigned to variables?

In the former case, you can use something like this:

for key, value in folder.items():
    print ("count of", key, "=", len(value))

In the latter case, the following will work for your particular case:

count_of_masterfolder = len(folder["Master folder"])
count_of_childfolder = len(folder["Child folder"])
inof
  • 465
  • 3
  • 7
0

You can try dict comprehension:

{k: len(v) for k,v in folders.items()}

Full example:

folders = {'Master folder': ['Training'], 'Child folder': [
    'Training videos', 'Training documents', 'Training workouts', 'Training practicals']}

print({k: len(v) for k,v in folders.items()})
# {'Master folder': 1, 'Child folder': 4}
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40
  • correct, but i need to store each item's count into a separate variables. is that possible? – ilexcel Aug 05 '19 at 15:44
  • It's a bad practise to create variable dynamically since it's hard to understand, debug, maintain and introduce security issues. A dictionary answers your question. You might have a look at [`eval`](https://docs.python.org/3/library/functions.html#eval) or [`exec`](https://docs.python.org/3/library/functions.html#exec). [*Some readings why they are bad practise*](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice). – Alexandre B. Aug 05 '19 at 15:49
0
folders = {'Master folder': ['Training'], 'Child folder': ['Training videos', 'Training documents', 'Training workouts', 'Training practicals']}

for key in list(folders.keys()):
  print(key, len(folders[key]))

>>>> Master folder 1
>>>>Child folder 4

EDIT: (to store outputs in list)

''''
mylist = []
for key in list(folders.keys()):
  print(key, len(folders[key]))
  mylist.extend(len(folders[key]))

Jkind9
  • 742
  • 7
  • 24
0

Why need complexity, when you can solve it using only len function

count_of_masterfolder = len(folders['Master folder'])
count_of_childfolder = len(folders['Child folder'])
Tony Montana
  • 921
  • 18
  • 46