Sep-28-2020, 07:05 AM
Hello all
I have created this list with dictionary for all video files with their media details. I want to remove all dictonaries records from the list where 'format' is not 'HEVC'.
Is there a better way? Do changes IN list rather than create another list.
Thanks
I have created this list with dictionary for all video files with their media details. I want to remove all dictonaries records from the list where 'format' is not 'HEVC'.
#!/usr/bin/env python3 old_list = [ {'fname': 'Welcome.mp4', 'format': 'HEVC', 'width': 1920, 'height': 1080, 'duration': 118.867, 'framerate': 30.0, 'fsize_byte': 4095489}, {'fname': 'tut.webm', 'format': 'VP9', 'width': 1152, 'height': 720, 'duration': 535.633, 'framerate': 23.976, 'fsize_byte': 17017448}, {'fname': 'Line.mp4', 'format': 'HEVC', 'width': 1920, 'height': 1080, 'duration': 141.8, 'framerate': 25.0, 'fsize_byte': 4495620}, {'fname': 'CAPITAL.MP4', 'format': 'AVC', 'width': 1280, 'height': 720, 'duration': 284.3, 'framerate': 30.0, 'fsize_byte': 26107717}, ] new_list = [] for i in old_list: print(i) for rec in old_list: if rec.get('format') != 'HEVC': new_list.append(rec) print() for j in new_list: print(j)The output is like this:
Output:{'fname': 'Welcome.mp4', 'format': 'HEVC', 'width': 1920, 'height': 1080, 'duration': 118.867, 'framerate': 30.0, 'fsize_byte': 4095489}
{'fname': 'tut.webm', 'format': 'VP9', 'width': 1152, 'height': 720, 'duration': 535.633, 'framerate': 23.976, 'fsize_byte': 17017448}
{'fname': 'Line.mp4', 'format': 'HEVC', 'width': 1920, 'height': 1080, 'duration': 141.8, 'framerate': 25.0, 'fsize_byte': 4495620}
{'fname': 'CAPITAL.MP4', 'format': 'AVC', 'width': 1280, 'height': 720, 'duration': 284.3, 'framerate': 30.0, 'fsize_byte': 26107717}
{'fname': 'tut.webm', 'format': 'VP9', 'width': 1152, 'height': 720, 'duration': 535.633, 'framerate': 23.976, 'fsize_byte': 17017448}
{'fname': 'CAPITAL.MP4', 'format': 'AVC', 'width': 1280, 'height': 720, 'duration': 284.3, 'framerate': 30.0, 'fsize_byte': 26107717}
Right now I'm using for loop to iterate through list and dictionary, check for value and append to another newly created list. Is there a better way? Do changes IN list rather than create another list.
Thanks