Search code examples
pythonpandasdictionary-comprehension

How shall I add one of the key and its values of second list of dictionary to the first list of dictionary based on the condition


From below lists, I want to add the 'Speed' from list_b to list_a if only 'Filename' of both lists matches. My output of newly formed list_a need to have 4 keys now and their respective values. How to do this?

first list of dictionary

list_a =[{'Filename': '20211004T102400622.jpg',
  'convertedDate': Timestamp('2021-10-04 10:24:00'),
  'path_name': '/content/drive/My Drive/train later/Continuous Running Time/Front Camera/20211004T102400622.jpg'},
 {'Filename': '20211004T102403688.jpg',
  'convertedDate': Timestamp('2021-10-04 10:24:03'),
  'path_name': '/content/drive/My Drive/train later/Continuous Running Time/Front Camera/20211004T102403688.jpg'},
 {'Filename': '20211004T102403684.jpg',
  'convertedDate': Timestamp('2021-10-04 10:24:03'),
  'path_name': '/content/drive/My Drive/train later/Continuous Running Time/Front Camera/20211004T102403684.jpg'},
 {'Filename': '20211004T102406737.jpg',
  'convertedDate': Timestamp('2021-10-04 10:24:06'),
  'path_name': '/content/drive/My Drive/train later/Continuous Running Time/Front Camera/20211004T102406737.jpg'},
 {'Filename': '20211004T102409773.jpg',
  'convertedDate': Timestamp('2021-10-04 10:24:09'),
  'path_name': '/content/drive/My Drive/train later/Continuous Running Time/Front Camera/20211004T102409773.jpg'}]

second list of dictionary

list_b = [{'Filename': '20211004T102400622.jpg',
  'Speed': '0.3',
  'Time': '2021-10-04T10:24:00.622Z'},
 {'Filename': '20211004T102403684.jpg',
  'Speed': '0.2',
  'Time': '2021-10-04T10:24:03.684Z'},
 {'Filename': '20211004T102403688.jpg',
  'Speed': '0.2',
  'Time': '2021-10-04T10:24:03.688Z'},
 {'Filename': '20211004T102406737.jpg',
  'Speed': '0.0',
  'Time': '2021-10-04T10:24:06.737Z'},
 {'Filename': '20211004T102409773.jpg',
  'Speed': '0.1',
  'Time': '2021-10-04T10:24:09.773Z'}]

Solution

  • Use dictionary comprehension to create dictionary filename_to_speed, for looking up speed for a given filename. Then build the new list using a loop. First check in the filename exists in filename_to_speed, otherwise add a value None.

    # I am guessing this is where Timestamp comes from.
    # Or import it the same way you did in your code (not shown)
    from pandas import Timestamp
    
    list_a =[{'Filename': '20211004T102400622.jpg',
      'convertedDate': Timestamp('2021-10-04 10:24:00'),
      'path_name': '/content/drive/My Drive/train later/Continuous Running Time/Front Camera/20211004T102400622.jpg'},
     {'Filename': '20211004T102403688.jpg',
      'convertedDate': Timestamp('2021-10-04 10:24:03'),
      'path_name': '/content/drive/My Drive/train later/Continuous Running Time/Front Camera/20211004T102403688.jpg'},
     {'Filename': '20211004T102403684.jpg',
      'convertedDate': Timestamp('2021-10-04 10:24:03'),
      'path_name': '/content/drive/My Drive/train later/Continuous Running Time/Front Camera/20211004T102403684.jpg'},
     {'Filename': '20211004T102406737.jpg',
      'convertedDate': Timestamp('2021-10-04 10:24:06'),
      'path_name': '/content/drive/My Drive/train later/Continuous Running Time/Front Camera/20211004T102406737.jpg'},
     {'Filename': '20211004T102409773.jpg',
      'convertedDate': Timestamp('2021-10-04 10:24:09'),
      'path_name': '/content/drive/My Drive/train later/Continuous Running Time/Front Camera/20211004T102409773.jpg'}]
    
    
    list_b = [{'Filename': '20211004T102400622.jpg',
      'Speed': '0.3',
      'Time': '2021-10-04T10:24:00.622Z'},
     {'Filename': '20211004T102403684.jpg',
      'Speed': '0.2',
      'Time': '2021-10-04T10:24:03.684Z'},
     {'Filename': '20211004T102403688.jpg',
      'Speed': '0.2',
      'Time': '2021-10-04T10:24:03.688Z'},
     {'Filename': '20211004T102406737.jpg',
      'Speed': '0.0',
      'Time': '2021-10-04T10:24:06.737Z'},
     {'Filename': '20211004T102409773.jpg',
      'Speed': '0.1',
      'Time': '2021-10-04T10:24:09.773Z'}]
    
    
    filename_to_speed = {dct['Filename'] : dct['Speed'] for dct in list_b}
    
    list_new = []
    
    for dct in list_a:
        if dct['Filename'] in filename_to_speed:
            dct['Speed'] = filename_to_speed[dct['Filename']]
        else:
            dct['Speed'] = None
        list_new.append(dct)
    
    print(list_new)
    # [{'Filename': '20211004T102400622.jpg', 'convertedDate': Timestamp('2021-10-04 10:24:00'), 'path_name': '/content/drive/My Drive/train later/Continuous Running Time/Front Camera/20211004T102400622.jpg', 'Speed': '0.3'}, {'Filename': '20211004T102403688.jpg', 'convertedDate': Timestamp('2021-10-04 10:24:03'), 'path_name': '/content/drive/My Drive/train later/Continuous Running Time/Front Camera/20211004T102403688.jpg', 'Speed': '0.2'}, {'Filename': '20211004T102403684.jpg', 'convertedDate': Timestamp('2021-10-04 10:24:03'), 'path_name': '/content/drive/My Drive/train later/Continuous Running Time/Front Camera/20211004T102403684.jpg', 'Speed': '0.2'}, {'Filename': '20211004T102406737.jpg', 'convertedDate': Timestamp('2021-10-04 10:24:06'), 'path_name': '/content/drive/My Drive/train later/Continuous Running Time/Front Camera/20211004T102406737.jpg', 'Speed': '0.0'}, {'Filename': '20211004T102409773.jpg', 'convertedDate': Timestamp('2021-10-04 10:24:09'), 'path_name': '/content/drive/My Drive/train later/Continuous Running Time/Front Camera/20211004T102409773.jpg', 'Speed': '0.1'}]