Getting Nan value in safe code

It's because you are dividing by zero. You are doing this because when the list is empty, this results in count being zero.