Skip to content

Instantly share code, notes, and snippets.

@mayankdiatm
Created March 28, 2020 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayankdiatm/46596533888a52b6c2db4e8e29066fb1 to your computer and use it in GitHub Desktop.
Save mayankdiatm/46596533888a52b6c2db4e8e29066fb1 to your computer and use it in GitHub Desktop.
Python Useful Snippets
collections.Counter()
A counter is a container that stores elements as dictionary keys, and their counts are stored as dictionary values.
>>> from collections import Counter
>>>
>>> myList = [1,1,2,3,4,5,3,2,3,4,2,1,2,3]
>>> print Counter(myList)
Counter({2: 4, 3: 4, 1: 3, 4: 2, 5: 1})
>>>
enumerate()
adds a counter to an iterable and returns it in a form of enumerate object. This enumerate object can then be used directly in for loops
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment