-2

I have this list

dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']

I need a function that would tell me that there is 3 gold coin, 1 dagger and 1 ruby in the dragon loot.

1 Answers1

1

You can use the Counter container.

from collections import Counter

c = Counter(dragonLoot)

for item in c:
    print(f'{item} {c[item]}')

This code would return: gold coin 3 dagger 1 ruby 1

Docs: https://docs.python.org/3/library/collections.html#collections.Counter

forayer
  • 367
  • 2
  • 10