Why do Python lists in my computer don't hold more than 693 numbers?
<p>I was solving the <a href="https://www.hackerrank.com/challenges/s10-basic-statistics" rel="nofollow">Day 0</a> of 10 Days of Statistics problems in HackerRank the other day. My solution was pretty straightforward and it worked with almost all the test cases. The one my code fails in has 2500 numbers as input. I placed a few prints to see how this is happening and discovered my list for the numbers holds only the 693 values of all.</p>

<p>Here is the part to find median:</p>

<pre><code>number_of_items = int(input().strip())
inputs = list(map(int, input().split(' ')))
inputs.sort()

if (number_of_items % 2) == 0:
    n = number_of_items // 2 - 1
    median = (inputs[n] + inputs[n+1])/2
else:
    n = number_of_items // 2
    median = inputs[n-1]
print(median)
</code></pre>

<p>Here is the full code: <a href="https://gist.github.com/mnzr/5a6f6c1c49d4dc0dbb940ed3ecba79ff" rel="nofollow">https://gist.github.com/mnzr/5a6f6c1c49d4dc0dbb940ed3ecba79ff</a></p>

<p>I have tried this code on an online editor and it worked, with an exception: the mode was way off from the actual number.</p>

<p>I thought Python lists can hold large amount of numbers! Why doesn't it work on my PC?</p>

<p>Edit: I have had a friend solve the problem on his own and relay me the results. Then told him to run my code and tell me what he sees. Here is his code: <a href="https://gist.github.com/sz-ashik440/192bc22b18da0292832e65997a6787a7" rel="nofollow">https://gist.github.com/sz-ashik440/192bc22b18da0292832e65997a6787a7</a>
Here is what happened:
1: His code worked, he said. I ran it and it worked on my PC too! For the first time that is.
2: I gave him my code and run it myself again. I saw there were only 693 elements, he reported the same.
3: And perhaps the most surprising thing is, his own version has gave the same out of index error and an array with the size of 693! My friend's own code on his own PC now gives wrong answers.</p>

<p>Here is my system config:</p>

<ul>
<li>Intel Core i5, 5th gen </li>
<li>8GB of RAM, 1600 MHz bus speed </li>
<li>Ubuntu 16.04.1</li>
<li>Python 3.5.2</li>
</ul>

<p>My friend is using Python 3.4.3 on Ubuntu 14.04.</p>

<p>I don't know why you are experiencing this limitation when running your code on your local machine, however, I strongly suspect that it is due to your input data - perhaps a stray new line character as suggested by <a href="http://stackoverflow.com/questions/39590384/why-do-python-lists-in-my-computer-dont-hold-more-than-693-numbers#comment66489821_39590384">Martijn Pieters</a>.</p>

<p>The problem that you have with HackerRank is due to your mode calculation:</p>

<pre><code># mode
occurances = {n: inputs.count(n) for n in inputs}
mode = min(occurances)
print(mode)
</code></pre>

<p>This always selects the lowest value from the input, not the lowest of the most frequently occurring values. Here is one way to do that:</p>

<pre><code>from collections import Counter

c = Counter(inputs)
max_count = c.most_common(1)[0][1]
print(sorted([x for x in c if c[x] == max_count])[0])
</code></pre>

<p>Note that there is also another problem, you need to round the mean and median values to 1 decimal place, but this error is not exposed by HackerRank's test data.</p>

python list python-3.x ubuntu