title: Flattening a list of dictionaries in Python
body: <p>I have a list of dictionaries with the following shape:</p>
  <pre><code>x = [{'website_ctr': [{'action_type': 'link_click', 'value': '0.481928'}],
    'campaign_id': 'x',
    'date_start': '2021-11-01',
    'date_stop': '2021-11-01',
    'account_id': 'y'},
   {'website_ctr': [{'action_type': 'link_click', 'value': '0.40568'}],
    'campaign_id': 'x',
    'date_start': '2021-11-01',
    'date_stop': '2021-11-01',
    'account_id': 'y'},
   {'website_ctr': [{'action_type': 'link_click', 'value': '0.711382'}],
    'campaign_id': 'x',
    'date_start': '2021-11-01',
    'date_stop': '2021-11-01',
    'account_id': 'y'}]
  </code></pre>
  <p>I'm trying flatten the dictionary, so that each object in the list looks something like this</p>
  <pre><code>{'website_ctr-0-action_type': 'link_click',
   'website_ctr-0-value': '0.481928',
   'campaign_id': 'x',
   'date_start': '2021-11-01',
   'date_stop': '2021-11-01',
   'account_id': 'y'}
  </code></pre>
  <p>I've found the following codesnippet, and tried to adapt it to my case, however I keep getting an <code>AttributeError: 'str' object has no attribute 'items'</code> error, which I assume comes from my first loop <code>for dictionary in datalist</code>.</p>
  <pre><code>import collections
  
  def flatten(datalist, parent_key=False, separator='-'):   
      items = []
      for dictionary in datalist:
          for key, value in dictionary.items():
              new_key = str(parent_key) + separator + key if parent_key else key
              if isinstance(value, collections.MutableMapping):
                  items.extend(flatten(value, new_key, separator).items())
              elif isinstance(value, list):
                  for k, v in enumerate(value):
                      items.extend(flatten({str(k): v}, new_key).items())
              else:
                  items.append((new_key, value))
      return dict(items)
  </code></pre>
  <p><strong>Question:</strong> What am I doing wrong?</p>
  <p>Full Traceback:</p>
  <pre><code>---------------------------------------------------------------------------
  AttributeError                            Traceback (most recent call last)
  c:\Users\avk\facebookdata\ReadData\fb_credentials.py in &lt;module&gt;
  ----&gt; 1 flatten(x)
  
  c:\Users\avk\facebookdata\ReadData\fb_credentials.py in flatten(datalist, parent_key, separator)
      102             elif isinstance(value, list):
      103                 for k, v in enumerate(value):
  --&gt; 104                     items.extend(flatten({str(k): v}, new_key).items())
      105             else:
      106                 items.append((new_key, value))
  
  c:\Users\avk\facebookdata\ReadData\fb_credentials.py in flatten(datalist, parent_key, separator)
       96     items = []
       97     for dictionary in datalist:
  ---&gt; 98         for key, value in dictionary.items():
       99             new_key = str(parent_key) + separator + key if parent_key else key
      100             if isinstance(value, collections.MutableMapping):
  
  AttributeError: 'str' object has no attribute 'items'
  </code></pre>