Pivot a column in list of dictionaries

Hi guys!
Things are starting to fall in place, thanks to you all!

There is one thing I’m not able to fix in my data.
I’ve got that nice list of dictionaries for my repeating panel.
But I would like to pivot/flip the elements of ‘username’ in 2 different column.

[{'period': 'Period 2020-08-01 to 2020-07-31', 'status': 'Open', 'username': 'Jane', 'amount': 180.32}, {'period': 'Period 2020-08-01 to 2020-07-31', 'status': 'Open', 'username': 'John', 'amount': 154.0}]

For example, I’m trying to get :

[{'period': 'Period 2020-08-01 to 2020-07-31', 'status': 'Open', 'amount_jane: '154.0', 'amount_john': 180.32}]

I found nothing on the forum.
I tried stackoverflow with no success.
I also tried stuff with zip and for loops condition.

Thanks in advance for your time!!

I guess it depends on the complexity of the data structure
you might find pandas pivot useful

or for your example something simpler like

my_table = [{'period': 'Period 2020-08-01 to 2020-07-31', 'status': 'Open', 'username': 'Jane', 'amount': 180.32}, 
            {'period': 'Period 2020-08-01 to 2020-07-31', 'status': 'Open', 'username': 'John', 'amount': 154.0}]
my_new_dict = {'amount'+row['username'] : row['amount'] for row in my_table}

Sorry for the delay, I was out of town!
Thanks sc for the answer, it indeed works well.

How can I keep my grouping for ‘period’ and ‘status’?
I tried to add variables to the dict but the editor flag an input error.

PS : I would definitely use pandas for my data operations but I’m still on the free plan for now :frowning:

There is still a way to use Pandas (or any other package you can install) on the free plan: have the Pandas-using function run on your own computer.

@p.colbert Yes! Thank you!

I’m aware of that functionality! :slight_smile:
It feel kind of weird using a remote machine just for basic data manipulation though.

You can use

my_new_dict.update({'date': somedate, 'status':somestatus})

You may also wish to check out itertools groupby

Here are some posts with related data manipulations and have itertools solutions

Thank you very much sc!
I will put that to use and report back! :slight_smile:

You might also be interested in this post from our blog:

@meredydd Thanks a lot for the article!
It is indeed always a good thing to think ahead!

It feels good to have such a great community! :slight_smile: