Efficient way of Creating dummy variables in python
<p>I want to create a vector of dummy variables(can take only O or 1). I am doing the following:</p>

<pre><code>data = ['one','two','three','four','six']
variables = ['two','five','ten']
</code></pre>

<p>I got the following two ways:</p>

<pre><code>dummy=[]
for variable in variables:
    if variable in data:
        dummy.append(1)
    else:
        dummy.append(0)
</code></pre>

<p>or with list comprehension:</p>

<pre><code>dummy = [1 if variable in data else 0 for variable in variables]
</code></pre>

<p>Results are ok:</p>

<pre><code>&gt;&gt;&gt; [1,0,0]
</code></pre>

<p>Is there a build in function doing this task quicker? Its kinda slow if the variables are thousands.</p>

<p><strong>Edit</strong>: Results using <code>time.time()</code>:
I am using the following data:</p>

<pre><code>data = ['one','two','three','four','six']*100
variables = ['two','five','ten']*100000
</code></pre>

<ul>
<li>Loop(from my example): 2.11 sec</li>
<li>list comprehension: 1.55 sec</li>
<li>list comprehension (variables are type of set): 0.0004992 sec</li>
<li>Example from Peter: 0.0004999 sec</li>
<li>Example from falsetrue: 0.000502 sec</li>
</ul>