For list of dicts: merge same key values, sum another diff. values, count each merge iteration +1
Question: I have list of dicts(or tuples), where:

if tuples:

    
    
     comment_id, user_id,     comment_date, comment_time, comment_likes
    ('51799',   '112801710', '2015-12-07',  '00:03:21',   '0'),
    ('51761',   '112801710', '2015-12-06',  '19:31:46',   '3'),
    ('51764',   '112801710', '2015-12-06',  '19:54:19',   '0'),
    ('51741',   '112801710', '2015-12-06',  '14:17:34',   '2'),
    ('51768',   '52879933',  '2015-12-06',  '20:03:34',   '0'),
    ('51766',   '52879933',  '2015-12-06',  '21:33:34',   '0'),
    

or can be converted to dict like:

    
    
    {'comm_count': 1, 'user_id': '217407103', 'likes': 0},
    

  * **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:

    
    
      user_id,      comment_likes,      comments_left
    ('112801710',   '5',                '4'),
    ('52879933',    '0',                '2')
    

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

**Examples of code:**

    
    
        for row in results:
        user_id = row[1]        # Get user id ['39411753']
        comm_id = row[0]        # Get post id ['51  575']
        comm_likes = row[4]     # Get post likes ['2']
        comm_likes = int(comm_likes)
        all_users_id_comments.append(user_id)
        if user_id not in temp_list:
            comm_count = 1
            temp_list.append(user_id)
            user_ids_dict = {'user_id':user_id,'likes':comm_likes,'comm_count':comm_count}
            result_dicts_list.append(user_ids_dict)
            if user_id in temp_list:
                for item in result_dicts_list:
                    if item['user_id'] == user_id:
                        item['comm_count'] += 1
                        item['likes'] += comm_likes
    

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:

    
    
    merged = {}
    for dict in user_comments_list_dicts:
    for key,value in dict.items():
        if key not in merged:
            merged [key] = []
        merged [key].append(value)
    print(merged)
    

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

    
    
    '144964044': [
              {'comm_id': '51640', 'likes': '0'},
              {'comm_id': '51607', 'likes': '0'},
              {'comm_id': '51613', 'likes': '0'},
              {'comm_id': '51591', 'likes': '1'},
              {'comm_id': '51592', 'likes': '0'},
              {'comm_id': '51317', 'likes': '0'},
              {'comm_id': '51319', 'likes': '0'},
              {'comm_id': '51323', 'likes': '0'}
              ],
    

But I can't call value "for 144964044" - it shows me just '144964044' but not
that list. Also confuses me.

Will be great to solve this with python, but IMHO this case can be solved also
on SQL db side, I don't know. Maybe I can UPDATE each row where user_id found
twice or more times and sum it likes and add +1 for each in comments_count.

Also python guys gave me an advice to use: comprehensions, sets, or key\value
- but I use them all - and still no result.

I want to be conscious novice, so I followed your advice about MySQL queries
and found this way:

    
    
    """SELECT SUM(comment_likes) AS value_sum, comment_user_id, COUNT(*)
                            FROM pub_comments_weekly
                            GROUP BY comment_user_id"""
    

This will show something like:

    
    
    ((7.0, '-80849532', 3), 
    (0.0, '100072457', 1), 
    (4.0, '10224064', 7), 
    (6.0, '10872377', 27), 
    (1.0, '111612257', 5), 
    (10.0, '112801710', 10), 
    (0.0, '112983834', 2), 
    (3.0, '11374187', 2), 
    (0.0, '11558683', 1), 
    (0.0, '118422944', 1), 
    (0.0, '119641064', 20), 
    (1.0, '119991466', 7), 
    (1.0, '121321268', 1), 
    (0.0, '12542463', 3))...
    

where: (likes, user_id, comments)

Thanks for helping!


Answer: counting and summing are most efficient done in the database using count, sum
functions and group by.

for some reason it is necessary that you do it in python, using a dictionary
will be my choice over tuples. also I recommend using dictionary of
dictionaries for the result data structure as it will make accessing it
easier.

    
    
    list = [ {'comment_id':'51799',   'user_id':'112801710', 'comment_date':'2015-12-07', 'comment_time': '00:03:21',   'comment_likes':'0'},
             {'comment_id':'51761',   'user_id':'112801710', 'comment_date':'2015-12-06',  'comment_time':'19:31:46',   'comment_likes':'3'},
             {'comment_id':'51764',   'user_id':'112801710', 'comment_date':'2015-12-06',  'comment_time':'19:54:19',   'comment_likes':'0'},
             {'comment_id':'51741',   'user_id':'112801710', 'comment_date':'2015-12-06',  'comment_time':'14:17:34',   'comment_likes':'2'},
             {'comment_id':'51768',   'user_id':'52879933',  'comment_date':'2015-12-06',  'comment_time':'20:03:34',   'comment_likes':'0'},
             {'comment_id':'51766',   'user_id':'52879933',  'comment_date':'2015-12-06',  'comment_time':'21:33:34',   'comment_likes':'0'}]
    
    
    def combine(list):
        result = {}
        for item in list:
            resItem = result.get(item['user_id'], None)
            if not resItem:
                resItem =  {'comment_likes': int(item['comment_likes']), 'comments_left': 1}
            else:
                resItem['comment_likes'] += int(item['comment_likes'])
                resItem['comments_left'] +=1
            result[item['user_id']] = resItem
    
        print result
    
    combine(list)
    

result:

    
    
    {'112801710': {'comment_likes': 5, 'comments_left': 4}, '52879933': {'comment_likes': 0, 'comments_left': 2}}
    

hope this helped.

