OiO.lk Blog python Creating an excel from a dict while group values
python

Creating an excel from a dict while group values


(I’m very new to coding)
I’m trying to create an excel from a list of dictionaries, the excel should contain

name, email, location.

the dictionary has duplicates of names and emails but locations are one time only used.

i want to build an excel where the name and email have their own row but all the location where the names are used are assigned to a single name

the dict items look like this, and i have ~2000 of them.

dict_list=[{‘UserName’: ‘name’, ‘Email’: ’email@email.com’, ‘location’: ‘x’}

end result should be

name | email@email.com | x ,y,c |

how should i build a list/dict that i can print to excel.

I’ve tried playing around with loop in a loop to check all the values against all the values of the second empty list, so if the entry doesn’t exist, it copies it from the first one.

filtered_List = []
dict_list=[{'UserName': 'name', 'Email': 'email@email.com', 'location': 'x'}]
for user in dict_list:
    for filtered_users in filtered_List:
        if user['UserName'] == filtered_users['UserName']:
            filtered_users['location'].append(user['location'])
            continue
        else:
            filtered_List.append(user)
            break



You need to sign in to view this answers

Exit mobile version