How to identify if __iter__ is called by dict or list?
<p>What I understand is <code>__iter__</code> method makes an object iterable. If it yields a pair, it produces a dictionary if called with <code>dict</code>. Now, if I want to create
class that creates a list of values if called with <code>list</code> and creates a dict if
called with <code>dict</code>, how to do that?</p>

<p>Let say the class is this:</p>

<pre><code>class Container(object):

    def __init__(self):
        self.pairs = [
           ('key1', 'val1'),
           ('key2', 'val2'),
        ]

    def __getitem__(self, idx):
        return self.pairs[idx][1]  # return value only

    def __iter__(self):
        for key, val in self.pairs:
            yield key, val
</code></pre>

<p>Now, if I use <code>list</code> or <code>dict</code>, I got:</p>

<pre><code>data = Container()
list(data)  # [('key1', 'val1'), ('key2', 'val2')]
dict(data)  # {'key1': 'val1', 'key2': 'val2'}
</code></pre>

<p>What I want (with <code>list</code>) is:</p>

<pre><code>list(data)  # ['val1', 'val2']
</code></pre>

<p>without keys.</p>

<p>Is it possible? If yes, how?</p>

<p>I think you have to do it manually with something like a list comprehension</p>

<pre><code>[v for k, v in data]
</code></pre>

python