Median implementation in python