Papouche Guinslyzinho
Papouche Guinslyzinho

Reputation: 5448

How to update values in list of dictionaries

I want to update the value of a key in dictionary. This is a snippet of a list that contains over 300 dictionaries

chats = [
{'hour': 10, 'operator': 'john_doe', 'duration': [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59], 'date': '2019-09-09'}, 
{'hour': 10, 'operator': 'john_doe', 'duration': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'date': '2019-09-09'},
{'hour': 10, 'operator': 'john_doe', 'duration': [18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28], 'date': '2019-09-09'},
{'hour': 11, 'operator': 'john_doe', 'duration': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28], 'date': '2019-09-09'},
{'hour': 10, 'operator': 'joseph_doe', 'duration': [5, 6, 7, 8, 9], 'date': '2019-09-09'}
]

script: I am getting an error on that script. I am looping to know if this dict is already in so that I can update the duration.

chat_list = list()
for chat in chats:
    hour = chat.get('hour')
    operator = chat.get("operator")
    if len(chat_list) == 0:
        chat_list.append(chat)
    else:
        found = False
        for i in chat_list:
            hour2  = chat.get('hour')
            operator2 = chat.get("operator")
            if (hour2 == hour) and (operator == operator2):
                found = True
                #concat both dictionary
                i['duration'] = i.get('duration') + chat.get("duration")
        if found == True:
            found = False
        else:
            chat_list.append(chat)

My expected output is

chat_list = [
{'hour': 10, 'operator': 'john_doe', 'duration': [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28], 'date': '2019-09-09'}, 
{'hour': 11, 'operator': 'john_doe', 'duration': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28], 'date': '2019-09-09'},
    {'hour': 10, 'operator': 'joseph_doe', 'duration': [5, 6, 7, 8, 9], 'date': '2019-09-09'}
        ]

or

df = pd.DataFrame(chat_list)
df['duration'] =  df['duration'].apply(lambda x: list(set(x)))

enter image description here

Upvotes: 0

Views: 60

Answers (1)

Raphael Medaer
Raphael Medaer

Reputation: 2538

To be honest, I didn't tested your algorithm. Instead I took it as a small challenge and I wrote the following algorithm which doesn't need to copy chats in to a new list.

It finds the first occurrence of "similar" chat and concat the duration arrays. Then it deletes the "duplicated" chat. Further explanation in the code itself:

chats = [
    {'hour': 10, 'operator': 'john_doe', 'duration': [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59], 'date': '2019-09-09'}, 
    {'hour': 10, 'operator': 'john_doe', 'duration': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'date': '2019-09-09'},
    {'hour': 10, 'operator': 'john_doe', 'duration': [18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28], 'date': '2019-09-09'},
    {'hour': 11, 'operator': 'john_doe', 'duration': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28], 'date': '2019-09-09'},
    {'hour': 10, 'operator': 'joseph_doe', 'duration': [5, 6, 7, 8, 9], 'date': '2019-09-09'}
]

index = 0
while index < len(chats) - 1:
    chat = chats[index]

    # detect if there is another "similar" chat in the list (before this one)
    first_index = next(
        i for i, first_chat in enumerate(chats)
        if chat.get('hour') == first_chat.get('hour') and chat.get('operator') == first_chat.get('operator')
    )

    # if the first index found is not this one:
    #   - concat `duration` arrays
    #   - delete this (duplicated) chat
    if index != first_index:
        chats[first_index]['duration'] += chat['duration']
        del chats[index]

    # otherwise continue and increment the index
    else:
        index += 1

print(chats)

Upvotes: 1

Related Questions