For list of dicts: merge same key values, sum another diff. values, count each merge iteration +1 Python

If anybody can help me with some task?

I have list of dicts(or tuples), where:

if tuples:

or can be converted to dict like:

  • comment_id – is always unique and cannot meet twice in this list,
  • user_id – is not unique for this list, it can be there as much times as comments were left in the set of posts (naturally I wanted to use this as counter)
  • comment_date and comment_time – can be ignored, needed to sel from db,
  • comment_likes – how much likes each comment has.

The task – make one list of tuples or dictionaries where I have just one ‘user_id’ unique, next how much ‘likes’ each item has (sum) and how much times this comment, with same user id was found in list.

To clarify, this is an expected result:

Somehow I made some different sets, but they does not work as expected.

Examples of code:

This way can make list where user_id meet only once and make dict with same user_ids but also with values. Then it checks list with all ids and if this id meet second time – update key values. But result is not correct, I’ve lost comething important.

Another good way to sort:

It make one set based on user_id with list of dicts for each comment, which user left:

But I can’t call value “for 144964044” – it shows me just ‘144964044’ but not that list. Also confuses me.



About trianglesis

Александр Брюндтзвельт - гений, филантроп, 100 гривен в кармане. Этот блог - "сток" моих мыслей и заметок. Достаточно одного взгляда на него, чтобы понять, что такой же бардак творится у меня в голове. Если вам этот бардак интересен - милости прошу.
Bookmark the permalink.