Unable to display the list of dictionaries of the form

[{ключ1:значение1, ключ2:значение21 },{ключ:значение2, ключ2:значение22}, {ключ:значение3, ключ2:значение21}..... и т.д.] 

Instead, the cycle runs over all values ​​and writes the last one to the list like this:

 [{ключ:значение3, ключ2:значение21}, {ключ:значение3, ключ2:значение21} {ключ:значение3, ключ2:значение21},{ключ:значение3, ключ2:значение21}, 

Tell me please what am I doing wrong?

 import os def scan_of_folder(): #получаем словарь all_path_to_files_in_music = {} a = [] tree = os.walk('music') for path, files, folders in tree: for files_in_folders in folders: all_path_to_files_in_music.update({'files_in_folders' : files_in_folders, 'path_to_file' : path}) a.append(all_path_to_files_in_music) return a if __name__ == '__main__': all_path_to_files_in_music = scan_of_folder() print(all_path_to_files_in_music) 

    1 answer 1

    try:

     def scan_of_folder(): #получаем словарь #all_path_to_files_in_music = {} a = [] tree = os.walk('selenium') for path, files, folders in tree: for files_in_folders in folders: all_path_to_files_in_music = {} # <------- all_path_to_files_in_music.update({'files_in_folders' : files_in_folders, 'path_to_file' : path}) a.append(all_path_to_files_in_music) return a 
    • Thanks, now it works as it should. Please tell me why the creation of a dictionary before cycles gives such an effect? - MrStepin