Reputation: 2881
I have a list which looks like this:
myList = [('string1', 48), ('string2', 48), ('string3', 18)]
how can I convert this into a counter? I've already this:
newCounter = Counter(myList)
but I seem to still be getting a list
Upvotes: 2
Views: 4523
Reputation: 13693
If you print newCounter
you'll be able to see it's already a counter object:
Counter({('string3', 18): 1, ('string1', 48): 1, ('string2', 48): 1})
Also see the type of newCounter
:
print type(newCounter)
<class 'collections.Counter'>
Upvotes: 1
Reputation: 280291
You're definitely not getting a list. You're not getting the Counter you want, because the Counter constructor will count an iterable it's provided instead of treating it as a list of key-value pairs, so turn the list into a dict first to get around that:
counter = Counter(dict(myList))
If you're sure you're getting a list, you have some other bug we can't see.
Upvotes: 6