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 stated in the comments, dictionaries need to have unique keys. So I would suggest a list of tuples.
To generate a similar form of the desired output, I suggest something like:</p>

<pre><code>ctr = 1

l = [
    'a',
    'a',
    'a',
    'a',
    'a',
    'a',
    'a',
    'a',
    'b',
    'b',
    'b',
    'b',
    'b',
    'b',
    'c',
    'c',
    'c',
    'c',
    'c',
]

out = []
for idx, element in enumerate(l):
    if idx == 0:
        out.append((element, ctr))
    else:
        if element != l[idx-1]:
            ctr = ctr + 1
        out.append((element, ctr))

print(out)
</code></pre>

<p>giving</p>

<pre><code>[('a', 1),
 ('a', 1),
 ('a', 1),
 ('a', 1),
 ('a', 1),
 ('a', 1),
 ('a', 1),
 ('a', 1),
 ('b', 2),
 ('b', 2),
 ('b', 2),
 ('b', 2),
 ('b', 2),
 ('b', 2),
 ('c', 3),
 ('c', 3),
 ('c', 3),
 ('c', 3),
 ('c', 3)]
</code></pre>

<hr>

<p><em>Update due to comment</em></p>

<p>The way lists are printed depends on which development environment you're using. However, to have something IDE-independent, this should to the job:</p>

<pre><code>for t in out:
    print(t)
</code></pre>

python dictionary