0

I need to return a dictionary the with the categories as keys and the counts as values.

I'm very new to coding but this is what I've come up with so far:

def bin_counter(num_list):
    num_dict = {}
    for num in num_list:
        count.num_dict[num] = 'value'
           
    return num_dict
Asocia
  • 5,935
  • 2
  • 21
  • 46
Anna
  • 19
  • 1

1 Answers1

0

If I understand your question right, this is the solution: you can use:

collections.Counter(num_list)

Or in case you what to understand how to implement the function,the following function will do the job:

def bin_counter(num_list):
        num_dict = {}
        for num in num_list:
                try:
                        num_dict[num] += 1
                except KeyError:
                        num_dict[num] = 1
        return num_dict

And if you don't familiar with Exeptions, the following function will do the same just without usage of Exeptions:

def bin_counter(num_list):
        num_dict = {}
        for num in num_list:
                if num in num_dict:
                        num_dict[num] += 1
                else:
                        num_dict[num] = 1
        return num_dict
Bigicecream
  • 159
  • 1
  • 6