By Rahul Agarwal

Published: 2019-04-22

URL: https://www.mlwhiz.com/p/python_defaultdict


Python Pro Tip: Start using Python defaultdict and Counter in place of dictionary

Learning a language is easy. Whenever I start with a new language, I focus on a few things in below order, and it is a breeze to get started with writing code in any language.

  • Operators and Data Types: +,-,int,float,str
  • Conditional statements: if,else,case,switch
  • Loops: For, while
  • Data structures: List, Array, Dict, Hashmaps
  • Define Function

However, learning to write a language and writing a language in an optimized way are two different things.

Every Language has some ingredients which make it unique.

Yet, a new programmer to any language will always do some forced overfitting. A Java programmer, new to python, for example, might write this code to add numbers in a list.


x=[1,2,3,4,5]
sum_x = 0
for i in range(len(x)):
sum_x+=x[i]

While a python programmer will naturally do this:


sum_x = sum(x)

In this series of posts named ‘Python Shorts’, I will explain some simple constructs that Python provides, some essential tips and some use cases I come up with regularly in my Data Science work.

Thi…