Find median of list of integers This code is meant to find the median of a list of integers. The list is assumed to be of odd length.

This is based of this HackerRank question.

I'm doing this to improve my style and to improve my knowledge of fundamental algorithms/data structures for an upcoming coding interview.

from heapq import heappush, heappop

def median(l):
    heap = []
    for i in l:
        heappush(heap, i)
    cur_int = None
    mid = len(l) // 2
    for i in range(0, mid+1):
        cur_int = heappop(heap)
    median = cur_int
    return median


I think this algorithm is \$O(n)\$ time and space complexity. Please correct me if I'm wrong. I'm also open to any suggestions to improving its complexities or general performance.
To clean up the code a bit and increase performance, try this one line that does everything your code does :-)

from statistics import median


As far as your algorithm goes, I think that the speed would likely be improved if you just sorted the list in the first place, then selected the middle one (or ones and took the average). You don't correctly handle the case where the size of the list is even.