Home > OS >  Can we delete the dictionary from list of dictionaries whose all key: ' ' (empty string)
Can we delete the dictionary from list of dictionaries whose all key: ' ' (empty string)

Time:03-05

Example:

reader = [{'Name': 'a', 'Created Date': '', 'Updated By': ''}, {'Name': 'b', 'Created Date': '', 'Updated By': ''}, {'Name': '', 'Created Date': '', 'Updated By': ''}]

expected result:

reader = [{'Name': 'a', 'Created Date': '', 'Updated By': ''}, {'Name': 'b', 'Created Date': '', 'Updated By': ''}]

I want to remove last dictionary whose all values are '' empty string.

CodePudding user response:

You could iterate your list of dicts and check if each dict has any value that has length greater than 0:

reader = [
    dict_element
    for dict_element in reader
    if any(value for value in dict_element.values())
]

Notice that using value instead of len(value) > 0 is equivalent since strings are casted to False only when the string is empty ("").

CodePudding user response:

use list comprehensions:

   reader = [_dict in reader if list(_dict.values())!=[''for i in len(_dict)]
      

CodePudding user response:

Use a comprehension:

out = [l for l in reader if any(l.values())]
print(out)

# Output
[{'Name': 'a', 'Created Date': '', 'Updated By': ''},
 {'Name': 'b', 'Created Date': '', 'Updated By': ''}]

CodePudding user response:

Here you go:

reader = [
    {'Amount': '12', 'Created Date': '', 'Updated By': '', 'Score': '', 'Updated Date': '', 'Company': 'neo', 'Email': '  ', 'Phone': '8002857', 'Fax': '', 'Created By': '', 'Name': '', 'Agent Owner': '', 'Source': '', 'Contact Name': 'XYZ'}, 
    {'Amount': '34', 'Created Date': '', 'Updated By': '', 'Score': '', 'Updated Date': '', 'Company': 'pop', 'Email': '', 'Phone': '8002857', 'Fax': '', 'Created By': '', 'Name': '', 'Agent Owner': '', 'Source': '', 'Contact Name': 'abc '}, 
    {'Amount': '', 'Created Date': '', 'Updated By': '', 'Score': '', 'Updated Date': '', 'Company': '', 'Email': '', 'Phone': '', 'Fax': '', 'Created By': '', 'Name': '', 'Agent Owner': '', 'Source': '', 'Contact Name': ''}
    ]        

reader = [row for row in reader if not all(v == '' for v in row.values())]
print(reader)

Output:

[
{'Amount': '12', 'Created Date': '', 'Updated By': '', 'Score': '', 'Updated Date': '', 'Company': 'neo', 'Email': '  ', 'Phone': '8002857', 'Fax': '', 'Created By': '', 'Name': '', 'Agent Owner': '', 'Source': '', 'Contact Name': 'XYZ'}, 
{'Amount': '34', 'Created Date': '', 'Updated By': '', 'Score': '', 'Updated Date': '', 'Company': 'pop', 'Email': '', 'Phone': '8002857', 'Fax': '', 'Created By': '', 'Name': '', 'Agent Owner': '', 'Source': '', 'Contact Name': 'abc '}
]

UPDATE: The question has been edited. Here is an update to the above answer which uses the latest sample input/output in the question:

reader = [
    {'Name': 'a', 'Created Date': '', 'Updated By': ''}, 
    {'Name': 'b', 'Created Date': '', 'Updated By': ''}, 
    {'Name': '', 'Created Date': '', 'Updated By': ''}
]
reader = [row for row in reader if not all(v == '' for v in row.values())]
print(reader)

Updated outputs:

[
{'Name': 'a', 'Created Date': '', 'Updated By': ''}, 
{'Name': 'b', 'Created Date': '', 'Updated By': ''}
]
  • Related