```
def thirdMax(self, nums):
l = [float('-inf')] * 3
for n in nums:
if n > l[0] and n not in l:
heapq.heappushpop(l, n)
return l[0] if l[0] != float('-inf') else max(l)
```

A slightly shorter but uglier version based on the same concept is:

```
def thirdMax(self, nums):
l = [float('-inf')] * 3
[heapq.heappushpop(l, n) for n in nums if n > l[0] and n not in l]
return l[0] if l[0] != float('-inf') else max(l)
```

Although I'm not sure whether the second version is still O(1) space. Does anyone know whether the Python interpreter will create the whole list comprehension in memory even though it is not being assigned anywhere?

Update: two line solution by @StefanPochmann below.