title: List of Dictionary Manipulation in python
body: <p>I have a list of the folowing form:  </p>
  
  <pre><code>oldlist = [{'x': 1, 'y':2},{'x':2, 'y':2},{'x':1, 'y':3},{'x':1, 'y':2},{'x':3, 'y':4}]  
  </code></pre>
  
  <p>to be converted into  </p>
  
  <pre><code>final = [{'x':1,'y':[2,3,2],'count':3},{'x':2,'y':[2],'count':1},{'x':3,'y':[4],'count':1}]  
  </code></pre>
  
  <p>I have tried  </p>
  
  <pre><code>oldlist = [{'x': {'a':1,'b':2}, 'y':2},{'x':{'a':6,'b':7}, 'y':2},{'x':{'a':1,'b':2}, 'y':3},{'x':{'a':1,'b':2}, 'y':2},{'x':{'a':10,'b':11}, 'y':4}]  
  list1=[]  
  list2=[]  
  list3=[]  
  s = set([d['x'] for d in oldlist])  
  news=list(s)  
  for item in oldlist:  
  if item['x'] == news[0]:  
        list1.append(item['y'])  
  
  if item['x'] == news[1]:  
        list2.append(item['y'])  
  
  if item['x'] == news[2]:  
        list3.append(item['y'])  
  final=[]  
  dic1 = {'x':news[0],'y':list1,'count':len(list1)}  
  dic2 = {'x':news[1],'y':list2,'count':len(list2)}  
  dic3 = {'x':news[2],'y':list3,'count':len(list3)}  
  final.append(dic1)  
  final.append(dic2)  
  final.append(dic3)  
  print final
  </code></pre>
  
  <p>Is there a simpler way to do it? Plus here I knew that x can have only three values so I created three variables list1, list2 and list3. What if x can have several other values and I have to find out a similar list of dictionaries like final! It should also work for strings!</p>