Python Forum
How to remove dict from a list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to remove dict from a list?
#1
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'.

#!/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
Reply


Messages In This Thread
How to remove dict from a list? - by Denial - Sep-28-2020, 07:05 AM
RE: How to remove dict from a list? - by buran - Sep-28-2020, 07:09 AM
RE: How to remove dict from a list? - by Denial - Sep-28-2020, 08:04 AM
RE: How to remove dict from a list? - by DeaD_EyE - Sep-28-2020, 08:19 AM
RE: How to remove dict from a list? - by buran - Sep-28-2020, 08:24 AM
RE: How to remove dict from a list? - by perfringo - Sep-28-2020, 09:10 AM
RE: How to remove dict from a list? - by buran - Sep-28-2020, 09:17 AM
RE: How to remove dict from a list? - by perfringo - Sep-28-2020, 02:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 443 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,335 Nov-13-2022, 01:27 AM
Last Post: menator01
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,216 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,462 May-31-2022, 08:43 PM
Last Post: Gribouillis
  Updating nested dict list keys tbaror 2 1,286 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Remove empty keys in a python list python_student 7 3,037 Jan-12-2022, 10:23 PM
Last Post: python_student
  Remove an item from a list contained in another item in python CompleteNewb 19 5,740 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,390 Jan-30-2021, 07:11 AM
Last Post: alloydog
  .remove() from a list - request for explanation InputOutput007 3 2,239 Jan-28-2021, 04:21 PM
Last Post: InputOutput007
Question dict value, how to change type from int to list? swissjoker 3 2,755 Dec-09-2020, 09:50 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020