How do I create a directory to assign values to similar items in a list.
<p>I need to create a dictionary where I can assign a value that classifies the same object in the list. Note that I don't have a preexisting value, I want python to assign one. Here is what I have:</p>

<pre><code> In [38]: post_title_list
 Out[38]: [u'the rfe master list',
           u'the rfe master list',
           u'the rfe master list', 
           ...]
</code></pre>

<p>the rfe master list goes on for about 700 more times until we begin the next title which is u'the co problem'.  I want to assign one number for each new phrase in the list, so the rfe master list is assigned 1 until we get to the co problem which would be assigned 2 and so on.</p>

<p>I have tried the following codes with no luck:</p>

<pre><code>  In [39]: d = dict(zip(post_title_list, range(len(post_title_list)))  
  Out[39]: {u'the rfe master list': '491818'}

  In [40]: {item: str(i) for i, item in enumerate(post_title_list)}
  Out[40]: {u'the rfe master list': '491818'}

  In [41]: dict.fromkeys(post_title_list)
  Out[41]: {u'the rfe master list': None}
</code></pre>

<p>Desired output:</p>

<pre><code>  Out[42]:  {u'the rfe master list': 1,
             u'the rfe master list': 1,
             u'the rfe master list': 1,
             u'the co problem'     : 2,
             u'the co problem'     : 2,
             u'expecting delays'   : 3,
             u'denied help         : 4,
             ...}
</code></pre>

<p>Thank you.</p>

<p>As it was already pointed out in the comments to your question, you can't have multiple entries for the same key in a dictionary.</p>

<p>One way to go would be a dictionary in which every title occurs only once and maps to the corresponding number:</p>

<pre><code>d = {}
next_id = 1
for title in post_title_list:
    if title not in d:
        d[title] = next_id
        next_id += 1
</code></pre>

<p>Alternatively, you can create a list with a tuple <code>(title,id)</code>for every element in your list:</p>

<pre><code>l = []
next_id = 0
last = None
for title in post_title_list:
    if title != last:
        next_id += 1
    l.append((title,next_id))
    last = title
</code></pre>

python dictionary