Python array_flip analogue or best way to do this?
<p>Is there array_flip (php) analogue for Python 3.x? </p>

<p>from</p>

<pre><code>obj = ['a', 'c', 'b' ]
</code></pre>

<p>to </p>

<pre><code>{'a': 1, 'c':2, 'b': 3}
</code></pre>

<p>You can use a list comprehension along with the <code>dict</code> constructor, as follows:</p>

<pre><code>&gt;&gt;&gt; obj = [ 'a', 'c', 'b' ]
&gt;&gt;&gt; dict((x, i + 1) for i, x in enumerate(obj))
{'a': 1, 'c': 2, 'b': 3}
</code></pre>

<p>As noted in the comments, you can also use a simple dict comprehension:</p>

<pre><code>&gt;&gt;&gt; { x: i + 1 for i, x in enumerate(obj) }
{'a': 1, 'c': 2, 'b': 3}
</code></pre>

python sorting