Python 3 | Sorted, Max, Min Dictionary trought a ZIP, Error
<p>I'm learning python 3 and I'm trying to use zip to transform a dictionary into a zip, this way I would be able to use functions like sorted, max and min on it.</p>

<p>Stocks is the dictionary btw.
So I tested it out like this, and it worked:</p>

<pre><code>print(min(zip(Stocks.values(),Stocks.keys())))
print(max(zip(Stocks.values(),Stocks.keys())))
print(sorted(zip(Stocks.values(),Stocks.keys())))
</code></pre>

<p>Then I tried to do this:</p>

<pre><code>stock_zip = zip(Stocks.values(), Stocks.keys())
print(max(stock_zip))
print(min(stock_zip))
print(sorted(stock_zip))
</code></pre>

<p>And this was the console result but I'm rather confused why it happened:</p>

<pre><code>(520, 'GOOG')
Traceback (most recent call last):
  File "----------------------------------------------------------------", line 11, in &lt;module&gt;
    print(min(stock_zip))
ValueError: min() arg is an empty sequence
</code></pre>

<p>If anyone can explain me why the second piece of code doens't work I would be very appreciated :)</p>

<p><code>zip</code> returns an iterator.</p>

<p>When you are calling <code>max(stock_zip)</code> it iterates and consume the <code>stock_zip</code> iterator. By the time <code>min(stock_zip)</code> is called, <code>stock_zip</code> is totally consumed and is empty.</p>

<p>Instead of saving a reference to the output of <code>zip(dict.keys(), dict.values())</code> you can simply use <code>dict.items()</code>:</p>

<pre><code>print(max(Stocks.items()))
print(min(Stocks.items()))
</code></pre>

python python-3.x