I am trying out the Counter method in python and then convert to dictionary. However, the list of elements is very large (~4200 elements). So the function is supposed to print the element and its frequency but it gives an error saying: ValueError: too many values to unpack (expected 2)
from collections import Counter
def contribution(competition):
users = Counter(['Filipe Andrew', 'M. B, Jnr, 'Retro P', 'Filipe Andrew', ...]) #up to 4200 elements
users_dict = dict(users)
for user, num_contribution in users_dict:
print(user, ' -> ', num_contribution)
contribution('year 2015_in_Brazil')```
CodePudding user response:
The items() method returns a view object. The view object contains the key-value pairs of the dictionary, as tuples in a list. The items method in a dictionary returns a dict items that is, dict_items([('Filipe Andrew', 1), ('M. B, Jnr, 1), ('Retro P', 1)])
The solution:
from collections import Counter
def contribution(competition):
users = Counter(['Filipe Andrew', 'M. B, Jnr, 'Retro P', 'Filipe Andrew', ...]) #up to 4200 elements
for user, num_contribution in users.items():
print(user, ' -> ', num_contribution)
contribution('year 2015_in_Brazil')