Skip to content
Advertisement

Is it possible to get binary count from Counter() Python

Using Counter(), I want to do a binary count of variables in a list. So instead of getting the count for each variable, I would simply like to have a Counter() variable, in which all values are one.

So for a given list:

JavaScript

I want the output to be:

JavaScript

Instead of:

JavaScript

I am aware that I could loop through the Counter:

JavaScript

However, that does require looping through the dictionary once and seems unnecessary to me.

Advertisement

Answer

This isn’t what a Counter is for, so whatever you do will require modifying the output.

Why not just not use a Counter to begin with? This should be trivial with a regular dict.

Heck, you can even just use dict.fromkeys, so:

JavaScript
Advertisement