Python Basic Tricks
#Python 1
How to check a Empty Dictionary in Python
You can easily check a python dictionary is Empty or not using bool operator. Suppose we have a dictionary dct.
- If bool(dct) is True then dict is not Empty
- If bool(dct) is False then dict is Empty
#Python 2
How to check a Memory Address in Python3
First consider some objects in python that we want to know their memory address:
id(object):
Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.
hex(x):
Convert an integer number to a lowercase hexadecimal string prefixed with “0x”. If x is not a Python int object, it has to define an index() method that returns an integer. Using hex we can easily know the address of the object reference. Some examples:
Using object.repr() we can easily know the object memory address with type of object.
#Python 3
How to find frequencies of all its unique elements in Python list or string?
Using python Collection module Counter we can easily find frequencies of all its unique elements.
Note: If you have any query or any upgradation of my writing or any mistakes please comment and suggest me. You are warmly welcomed always.