Search code examples
pythonlistdictionary

How to add each item in a list to the end of each dictionary item in a list of dictionarys


I have two arrays, like below:

user_data = [{'a': '1', 'b': '3'}, {'a': '2', 'b': '4'}, {'a': '5', 'b': '1'}]
available_times_array = ['12:00', '12:10', '12:20']

And I'm waiting to have this result array:

user_data = [
    {'a': '1', 'b': '3', 'c': '12:00'},
    {'a': '2', 'b': '4', 'c': '12:10'},
    {'a': '5', 'b': '1', 'c': '12:20'}]

Solution

  • user_data = [{'a': '1', 'b': '3'}, {'a': '2', 'b': '4'}, {'a': '5', 'b': '1'}]
    available_times_array = ['12:00', '12:10', '12:20']
    
    for data, time in zip(user_data, available_times_array):
        data['c'] = time
    
    print(user_data)